Last active
March 13, 2024 18:08
-
-
Save manuelpuyol/dbb1e41c748dbaf80da3ca3556648611 to your computer and use it in GitHub Desktop.
Intl demo for Hermes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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