Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Last active August 23, 2016 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattpodwysocki/22d0939bcb984b45f2f5037ba0990fc8 to your computer and use it in GitHub Desktop.
Save mattpodwysocki/22d0939bcb984b45f2f5037ba0990fc8 to your computer and use it in GitHub Desktop.
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