ChakraCore can be embedded via JavaScript Runtime (JSRT) APIs. This document goes through the basics of embedding with a Hello-world sample to get you started. To learn more about JSRT, visit JavaScript Runtime (JSRT) Overview.
You should first clone and build ChakraCore. There are a few files that you will need,
lib\jsrt\
, which are the headers.Build\VcBuild\bin\[platform+output]\
.C# users only need ChakraCore.dll.
Use JSRT with Visual StudioTo use JSRT in a C++ project:
#include "ChakraCore.h"
in your project.<your project> > Properties > Configuration Properties > Linker > Input > Additional Dependencies
, and add a reference to ChakraCore.lib.To use JSRT in a C# project:
[DllImport("ChakraCore.dll")]
internal static extern JavaScriptErrorCode JsCreateRuntime(JavaScriptRuntimeAttributes attributes, JavaScriptThreadServiceCallback threadService, out JavaScriptRuntime runtime);
Alternatively, you can also try using a higher level .NET wrapper.
A sample to help you understand how to embed ChakraCore with JSRT APIs. For C# users, please refer to the next section.
#include "ChakraCore.h"
#include <string>
#include <iostream>
using namespace std;
int main()
{
JsRuntimeHandle runtime;
JsContextRef context;
JsValueRef result;
unsigned currentSourceContext = 0;
// Your script; try replace hello-world with something else
wstring script = L"(()=>{return \'Hello world!\';})()";
// Create a runtime.
JsCreateRuntime(JsRuntimeAttributeNone, nullptr, &runtime);
// Create an execution context.
JsCreateContext(runtime, &context);
// Now set the current execution context.
JsSetCurrentContext(context);
// Run the script.
JsRunScript(script.c_str(), currentSourceContext++, L"", &result);
// Convert your script result to String in JavaScript; redundant if your script returns a String
JsValueRef resultJSString;
JsConvertValueToString(result, &resultJSString);
// Project script result back to C++.
const wchar_t *resultWC;
size_t stringLength;
JsStringToPointer(resultJSString, &resultWC, &stringLength);
wstring resultW(resultWC);
cout << string(resultW.begin(), resultW.end()) << endl;
system("pause");
// Dispose runtime
JsSetCurrentContext(JS_INVALID_REFERENCE);
JsDisposeRuntime(runtime);
return 0;
}
To build and run this sample,
F6
or using Build > Build Solution
. Make sure the build targets the same platform as the ChakraCore.dll you built earlier.Ctrl+F5
or using Debug > Start Without Debugging
.C# version of the above Hello-world sample. Note that JSRT APIs are C++ APIs, this sample assumes a C# wrapper.
using System;
using System.Runtime.InteropServices;
// wrapper namespace
using ChakraHost.Hosting;
public class HelloWorld
{
static void Main() {
JavaScriptRuntime runtime;
JavaScriptContext context;
JavaScriptSourceContext currentSourceContext = JavaScriptSourceContext.FromIntPtr(IntPtr.Zero);
JavaScriptValue result;
// Your script, try replace the basic hello world with something else
string script = "(()=>{return \'Hello world!\';})()";
// Create a runtime.
Native.JsCreateRuntime(JavaScriptRuntimeAttributes.None, null, out runtime);
// Create an execution context.
Native.JsCreateContext(runtime, out context);
// Now set the execution context as being the current one on this thread.
Native.JsSetCurrentContext(context);
// Run the script.
Native.JsRunScript(script, currentSourceContext++, "", out result);
// Convert your script result to String in JavaScript; redundant if your script returns a String
JavaScriptValue resultJSString;
Native.JsConvertValueToString(result, out resultJSString);
// Project script result in JS back to C#.
IntPtr resultPtr;
UIntPtr stringLength;
Native.JsStringToPointer(resultJSString, out resultPtr, out stringLength);
string resultString = Marshal.PtrToStringUni(resultPtr);
Console.WriteLine(resultString);
Console.ReadLine();
// Dispose runtime
Native.JsSetCurrentContext(JavaScriptContext.Invalid);
Native.JsDisposeRuntime(runtime);
}
}
To build and run this sample,
F6
or using Build > Build Solution
. Make sure the build targets the same platform as the ChakraCore.dll you built earlier.Ctrl+F5
or using Debug > Start Without Debugging
.For Linux/OSX, see Building ChakraCore and
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4