Skip to content

Instantly share code, notes, and snippets.

@manuelpuyol
Last active March 13, 2024 18:08
Show Gist options
  • Save manuelpuyol/dbb1e41c748dbaf80da3ca3556648611 to your computer and use it in GitHub Desktop.
Save manuelpuyol/dbb1e41c748dbaf80da3ca3556648611 to your computer and use it in GitHub Desktop.
Intl demo for Hermes
// based on https://github.com/tmikov/hermes-jsi-demos/blob/master/hello/hello.cpp
// Intl calls from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat
#include <hermes/hermes.h>
#include <iostream>
/// JS code to be executed.
static const char *code = R"(
const date = new Date(Date.UTC(2020, 11, 20, 3, 23, 16, 738));
print('Expected output: "12/20/2020"')
print('Actual output:', new Intl.DateTimeFormat('en-US').format(date));
print('Expected output: "20/12/2020"')
print('Actual output:', new Intl.DateTimeFormat(['ban', 'id']).format(date));
print('Expected output: "Sunday, 20 December 2020 at 14:23:16 GMT+11"')
print(
'Actual output:',
new Intl.DateTimeFormat('en-GB', {
dateStyle: 'full',
timeStyle: 'long',
timeZone: 'Australia/Sydney',
}).format(date),
);
)";
int main() {
// You can Customize the runtime config here.
auto runtimeConfig =
hermes::vm::RuntimeConfig::Builder().withIntl(true).build();
// Create the Hermes runtime.
auto runtime = facebook::hermes::makeHermesRuntime(runtimeConfig);
// Execute some JS.
int status = 0;
try {
runtime->evaluateJavaScript(
std::make_unique<facebook::jsi::StringBuffer>(code), "main.js");
} catch (facebook::jsi::JSError &e) {
// Handle JS exceptions here.
std::cerr << "JS Exception: " << e.getStack() << std::endl;
status = 1;
} catch (facebook::jsi::JSIException &e) {
// Handle JSI exceptions here.
std::cerr << "JSI Exception: " << e.what() << std::endl;
status = 1;
}
return status;
}
Expected output: "12/20/2020"
Actual output: 1608434596738.000000
Expected output: "20/12/2020"
Actual output: 1608434596738.000000
Expected output: "Sunday, 20 December 2020 at 14:23:16 GMT+11"
Actual output: 1608434596738.000000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment