-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
95 lines (79 loc) · 4.34 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
namespace ChakraSharp_Sample
{
using ChakraSharp;
using System;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
//This sample application demonstrates how to use the generated ChakraCore PInvoke layer to execute a sample script.
//Although a higher level object model in .net can be easily created that eliminates the need for boilerplate code,
//this sample focuses on direct, low-level usage of the ChakraCore API.
//Create a new ChakraCore JS runtime.
IntPtr runtimePtr;
var result = ChakraCommon.JsCreateRuntime(JsRuntimeAttributes.JsRuntimeAttributeNone, null, out runtimePtr);
Debug.Assert(result == JsErrorCode.JsNoError);
//Create a context
IntPtr contextPtr;
result = ChakraCommon.JsCreateContext(runtimePtr, out contextPtr);
Debug.Assert(result == JsErrorCode.JsNoError);
//Set the current context on the runtime -- multiple contexts can be created, but only one can be active on a runtime at a time.
result = ChakraCommon.JsSetCurrentContext(contextPtr);
Debug.Assert(result == JsErrorCode.JsNoError);
//Create a string that contains a small description of our code. This string shows up when exceptions occur, in a full application this probably should indicate the physical location of the script.
var sourceUrl = "[eval code]";
IntPtr sourceUrlPtr;
result = ChakraCore.JsCreateString(sourceUrl, (ulong)sourceUrl.Length, out sourceUrlPtr);
Debug.Assert(result == JsErrorCode.JsNoError);
//Create the actual script that we're going to execute.
var script = "6*7";
IntPtr scriptPtr;
result = ChakraCore.JsCreateString(script, (ulong)script.Length, out scriptPtr);
Debug.Assert(result == JsErrorCode.JsNoError);
//Tell the JavaScript engine to execute our script, associating the sourceUrl we created with it.
IntPtr resultPtr;
result = ChakraCore.JsRun(scriptPtr, 0, sourceUrlPtr, JsParseScriptAttributes.JsParseScriptAttributeNone, out resultPtr);
Debug.Assert(result == JsErrorCode.JsNoError);
//Convert the result (which is a number) to a string.
IntPtr stringResultPtr;
result = ChakraCommon.JsConvertValueToString(resultPtr, out stringResultPtr);
Debug.Assert(result == JsErrorCode.JsNoError);
//Obtain and copy the string from the runtime to a local variable.
string strResult;
unsafe
{
uint written;
sbyte[] buffer = new sbyte[0];
ulong targetLength;
//Determine the length of the string by invoking JsCopyString with a 0 length.
fixed (sbyte* bufferPtr = buffer)
{
result = ChakraCore.JsCopyString(stringResultPtr, bufferPtr, 0, out written);
Debug.Assert(result == JsErrorCode.JsNoError);
targetLength = written;
}
//Create the buffer that will store the string and invoke JsCopyString with the length of our buffer.
buffer = new sbyte[targetLength];
fixed (sbyte* bufferPtr = buffer)
{
result = ChakraCore.JsCopyString(stringResultPtr, bufferPtr, (ulong)buffer.LongLength, out written);
Debug.Assert(result == JsErrorCode.JsNoError);
//Create a new string from the sbyte buffer.
strResult = new string(bufferPtr, 0, (int)written);
}
}
//As we're all done, set the current context to nothing.
result = ChakraCommon.JsSetCurrentContext(IntPtr.Zero);
Debug.Assert(result == JsErrorCode.JsNoError);
//Dispose of the runtime.
result = ChakraCommon.JsDisposeRuntime(runtimePtr);
Debug.Assert(result == JsErrorCode.JsNoError);
//Output what we got.
Console.WriteLine(string.Format("The result of {0} is {1}", script, strResult));
Console.WriteLine();
Console.WriteLine("Press enter to continue...");
Console.ReadLine();
}
}
}