NO

Author Topic: Microsoft ChakraCore  (Read 2281 times)

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Microsoft ChakraCore
« on: October 06, 2017, 11:14:19 AM »
ChakraCore
ChakraCore/releases
Embedding ChakraCore

HelloWorld example:
Code: [Select]
//#include "ChakraCore.h"
//#include <string>
//#include <iostream>

//using namespace std;

#ifdef __POCC__
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <wchar.h>
#include <stdbool.h>
#include <stddef.h>  // for size_t
#include <stdint.h>  // for uintptr_t
#include <stdlib.h>  // for system()

#pragma comment(lib, "ChakraCore.lib")

// ChakraCommon.h
typedef uintptr_t ChakraCookie;
typedef unsigned char* ChakraBytePtr;

typedef void *JsRuntimeHandle;
typedef void *JsRef;
typedef JsRef JsContextRef;
typedef JsRef JsValueRef;
typedef ChakraCookie JsSourceContext;
#define JS_INVALID_REFERENCE (JsRef)0
typedef enum _JsErrorCode {
JsNoError = 0,
} JsErrorCode;
typedef enum _JsRuntimeAttributes {
JsRuntimeAttributeNone = 0x00000000,
} JsRuntimeAttributes;
#define CHAKRA_CALLBACK CALLBACK
#define CHAKRA_API STDAPI_(JsErrorCode)

typedef void (CHAKRA_CALLBACK *JsBackgroundWorkItemCallback)(void *callbackState);
typedef bool (CHAKRA_CALLBACK *JsThreadServiceCallback)(JsBackgroundWorkItemCallback callback, void *callbackState);

CHAKRA_API JsCreateRuntime(JsRuntimeAttributes attributes, JsThreadServiceCallback threadService, JsRuntimeHandle *runtime);
CHAKRA_API JsCreateContext(JsRuntimeHandle runtime, JsContextRef *newContext);
CHAKRA_API JsGetCurrentContext(JsContextRef *currentContext);
CHAKRA_API JsSetCurrentContext(JsContextRef context);
CHAKRA_API JsConvertValueToString(JsValueRef value, JsValueRef *stringValue);
CHAKRA_API JsDisposeRuntime(JsRuntimeHandle runtime);

// ChakraCommonWindows.h
CHAKRA_API JsRunScript(const wchar_t *script, JsSourceContext sourceContext, const wchar_t *sourceUrl, JsValueRef *result);
CHAKRA_API JsStringToPointer(JsValueRef value, const wchar_t **stringValue, size_t *stringLength);

#endif
//int main()

int __cdecl main(int argc, char **argv)
{
JsRuntimeHandle runtime;
JsContextRef context;
JsValueRef result;
unsigned currentSourceContext = 0;
void* nullptr = 0;

// Your script; try replace hello-world with something else
//    wstring script = L"(()=>{return \'Hello world!\';})()";
wchar_t *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, 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;
wprintf(L"%ls\n", resultWC);
system("pause");

// Dispose runtime
JsSetCurrentContext(JS_INVALID_REFERENCE);
JsDisposeRuntime(runtime);

return 0;
}
ChakraCore.dll
« Last Edit: October 06, 2017, 03:47:24 PM by TimoVJL »
May the source be with you