Last active
August 23, 2016 18:11
-
-
Save mattpodwysocki/22d0939bcb984b45f2f5037ba0990fc8 to your computer and use it in GitHub Desktop.
This file contains 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
int SerializeScript() | |
{ | |
ChakraHost host; | |
JsErrorCode status = JsNoError; | |
status = host.Init(); | |
if (status != JsNoError) | |
{ | |
wprintf(L"Error in initializing runtime\n"); | |
goto cleanup; | |
} | |
const wchar_t* szFile = L"lodash.js"; | |
const wchar_t* szSerializedFile = L"lodash.bin"; | |
JsValueRef lodash; | |
IfFailCleanup(host.SerializeScriptFromFile(szFile, szSerializedFile)); | |
IfFailCleanup(host.RunSerializedScriptFromFile(szSerializedFile, szFile, L"", &lodash)); | |
JsPropertyIdRef lodashId; | |
JsValueRef lodashObj; | |
IfFailCleanup(JsGetPropertyIdFromName(L"_", &lodashId)); | |
IfFailCleanup(JsGetProperty(host.globalObject, lodashId, &lodashObj)); | |
JsPropertyIdRef addId; | |
IfFailCleanup(JsGetPropertyIdFromName(L"add", &addId)); | |
JsValueRef addFunction; | |
IfFailCleanup(JsGetProperty(lodashObj, addId, &addFunction)); | |
JsValueRef arg1, arg2, result; | |
IfFailCleanup(JsIntToNumber(12, &arg1)); | |
IfFailCleanup(JsIntToNumber(34, &arg2)); | |
JsValueRef args[] = { lodashObj, arg1, arg2 }; | |
IfFailCleanup(JsCallFunction(addFunction, args, 3, &result)); | |
double resultValue; | |
IfFailCleanup(JsNumberToDouble(result, &resultValue)); | |
wprintf(L"Result: %f", resultValue); | |
cleanup: | |
status = host.Destroy(); | |
if (status != JsNoError) | |
{ | |
wprintf(L"Error in destroying runtime"); | |
} | |
return status; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment