From 3ec2f4fc6ef27f6f12e85ebb2aed96db5c503c2b Mon Sep 17 00:00:00 2001 From: Sandeep Agarwal Date: Tue, 15 Nov 2016 12:20:29 -0800 Subject: [PATCH] chakrashim: Fix JsParse/JsRun APIs PR-URL: nodejs/node-chakracore#140 Reviewed-By: Hitesh Kanwathirtha Reviewed-By: Jianchun Xu Reviewed-By: Kunal Pathak --- deps/chakrashim/core/lib/Jsrt/Jsrt.cpp | 65 +++++++++++++++----------- 1 file changed, 38 insertions(+), 27 deletions(-) diff --git a/deps/chakrashim/core/lib/Jsrt/Jsrt.cpp b/deps/chakrashim/core/lib/Jsrt/Jsrt.cpp index 0bb128b5419..64547008268 100644 --- a/deps/chakrashim/core/lib/Jsrt/Jsrt.cpp +++ b/deps/chakrashim/core/lib/Jsrt/Jsrt.cpp @@ -4277,11 +4277,28 @@ _ALWAYSINLINE JsErrorCode CompileRun( { PARAM_NOT_NULL(scriptVal); VALIDATE_JSREF(scriptVal); + PARAM_NOT_NULL(sourceUrl); bool isExternalArray = Js::ExternalArrayBuffer::Is(scriptVal), isString = false; bool isUtf8 = !(parseAttributes & JsParseScriptAttributeArrayBufferIsUtf16Encoded); - if (!isExternalArray) + + LoadScriptFlag scriptFlag = LoadScriptFlag_None; + const byte* script; + size_t cb; + const wchar_t *url; + + if (isExternalArray) + { + script = ((Js::ExternalArrayBuffer*)(scriptVal))->GetBuffer(); + + cb = ((Js::ExternalArrayBuffer*)(scriptVal))->GetByteLength(); + + scriptFlag = (LoadScriptFlag)(isUtf8 ? + LoadScriptFlag_ExternalArrayBuffer | LoadScriptFlag_Utf8Source : + LoadScriptFlag_ExternalArrayBuffer); + } + else { isString = Js::JavascriptString::Is(scriptVal); if (!isString) @@ -4290,36 +4307,30 @@ _ALWAYSINLINE JsErrorCode CompileRun( } } - LoadScriptFlag scriptFlag; - const byte* script = isExternalArray ? - ((Js::ExternalArrayBuffer*)(scriptVal))->GetBuffer() : - (const byte*)((Js::JavascriptString*)(scriptVal))->GetSz(); - const size_t cb = isExternalArray ? - ((Js::ExternalArrayBuffer*)(scriptVal))->GetByteLength() : - ((Js::JavascriptString*)(scriptVal))->GetLength(); + JsErrorCode error = GlobalAPIWrapper_NoRecord([&]() -> JsErrorCode { + if (isString) + { + Js::JavascriptString* jsString = Js::JavascriptString::FromVar(scriptVal); + script = (const byte*)jsString->GetSz(); - if (isExternalArray && isUtf8) - { - scriptFlag = (LoadScriptFlag) (LoadScriptFlag_ExternalArrayBuffer | LoadScriptFlag_Utf8Source); - } - else if (isUtf8) - { - scriptFlag = (LoadScriptFlag) (LoadScriptFlag_Utf8Source); - } - else - { - scriptFlag = LoadScriptFlag_None; - } + // JavascriptString is 2 bytes (wchar_t/char16) + cb = jsString->GetLength() * sizeof(wchar_t); + } - const wchar_t *url; + if (!Js::JavascriptString::Is(sourceUrl)) + { + return JsErrorInvalidArgument; + } - if (sourceUrl && Js::JavascriptString::Is(sourceUrl)) - { - url = ((Js::JavascriptString*)(sourceUrl))->GetSz(); - } - else + url = Js::JavascriptString::FromVar(sourceUrl)->GetSz(); + + return JsNoError; + + }); + + if (error != JsNoError) { - return JsErrorInvalidArgument; + return error; } return RunScriptCore(scriptVal, script, cb, scriptFlag,