public void JsParseDoesParse() { var script = "(()=>{return 6*7;})()"; string sourceUrl = "[eval code]"; using (var runtimeHandle = Engine.JsCreateRuntime(JavaScriptRuntimeAttributes.None, null)) { using (var contextHandle = Engine.JsCreateContext(runtimeHandle)) { Engine.JsSetCurrentContext(contextHandle); IntPtr ptrScript = Marshal.StringToHGlobalAnsi(script); try { var scriptHandle = Engine.JsCreateExternalArrayBuffer(ptrScript, (uint)script.Length, null, IntPtr.Zero); var sourceContext = JavaScriptSourceContext.None; var sourceUrlHandle = Engine.JsCreateString(sourceUrl, (ulong)sourceUrl.Length); var resultHandle = Engine.JsParse(scriptHandle, sourceContext, sourceUrlHandle, JavaScriptParseScriptAttributes.None); Assert.True(resultHandle != JavaScriptValueSafeHandle.Invalid); var handleType = Engine.JsGetValueType(resultHandle); Assert.True(handleType == JsValueType.Function); resultHandle.Dispose(); sourceUrlHandle.Dispose(); scriptHandle.Dispose(); } finally { Marshal.FreeHGlobal(ptrScript); } } } }
public ScriptSource(IJavaScriptEngine engine, string script, string sourceUrl = "[eval source]") { if (engine == null) { throw new ArgumentNullException(nameof(engine)); } m_script = script ?? throw new ArgumentNullException(nameof(script)); m_ptrScript = Marshal.StringToHGlobalAnsi(script); m_scriptHandle = engine.JsCreateExternalArrayBuffer(m_ptrScript, (uint)script.Length, null, IntPtr.Zero); m_sourceUrlHandle = engine.JsCreateString(sourceUrl, (ulong)sourceUrl.Length); m_sourceContext = JavaScriptSourceContext.GetNextSourceContext(); m_fnScript = engine.JsParse(m_scriptHandle, m_sourceContext, m_sourceUrlHandle, JavaScriptParseScriptAttributes.None); }