■
ChakraからCOMやVARIANTが消えたために、またやり直しだ。こちらが主流になると思われる。
以下はC++によるArrayとMapへの操作例。
#include "stdafx.h"
#define _JSRT_
#include "ChakraCore.h"
#include
#include
#include
#pragma comment (lib, "chakracore")
using namespace std;
// array
int main()
{
JsRuntimeHandle runtime;
JsContextRef context;
JsValueRef result;
unsigned currentSourceContext = 0;
// Your script, try replace the basic hello world with something else
wstring script = L"(()=>{ var a = [1,2,3]; return a;})()"; // <-- array
// Create a runtime.
JsCreateRuntime(JsRuntimeAttributeNone, nullptr, &runtime);
// Create an execution context.
JsCreateContext(runtime, &context);
// Now set the execution context as being the current one on this thread.
JsSetCurrentContext(context);
// Run the script.
JsRunScript(script.c_str(), currentSourceContext++, L"", &result);
JsValueType ty;
auto ret = JsGetValueType(result, &ty ); // 8: JsArray
JsValueRef jsobj;
ret = JsConvertValueToObject(result, &jsobj );
JsValueRef xnm;
JsGetOwnPropertyNames( jsobj, &xnm );
JsValueRef lengthName,lengthValue;
ret = JsGetPropertyIdFromName(L"length", &lengthName);
ret = JsGetProperty(xnm, lengthName, &lengthValue);
int count;
JsNumberToInt(lengthValue, &count);
wcout << L'[';
for( int i = 0; i < count-1; i++ )
{
JsValueRef index;
JsIntToNumber(i, &index );
JsValueRef IndexedProperty;
JsGetIndexedProperty(jsobj, index, &IndexedProperty );
JsValueRef itemref;
JsConvertValueToString( IndexedProperty, &itemref );
WCHAR* cb; UINT len;
ret = JsStringToPointer( itemref, (const wchar_t**)&cb, &len );
wcout << cb << L',';
}
wcout << L']' << endl;
// output is [1,2,3].
system("pause");
// Dispose runtime
JsSetCurrentContext(JS_INVALID_REFERENCE);
JsDisposeRuntime(runtime);
return 0;
}
// map
int main()
{
JsRuntimeHandle runtime;
JsContextRef context;
JsValueRef result;
unsigned currentSourceContext = 0;
// Your script, try replace the basic hello world with something else
wstring script = L"(()=>{ var a = { 'tokyo':'03', 'yokohama':'045', 'oosaka':'06'}; return a;})()"; // <-- map
// Create a runtime.
JsCreateRuntime(JsRuntimeAttributeNone, nullptr, &runtime);
// Create an execution context.
JsCreateContext(runtime, &context);
// Now set the execution context as being the current one on this thread.
JsSetCurrentContext(context);
// Run the script.
JsRunScript(script.c_str(), currentSourceContext++, L"", &result);
JsValueRef jsobj;
JsValueType ty;
auto ret = JsGetValueType(result, &ty ); // 5: JsObject
ret = JsConvertValueToObject(result, &jsobj );
JsValueRef xnm;
JsGetOwnPropertyNames( jsobj, &xnm );
JsValueRef lengthName,lengthValue;
ret = JsGetPropertyIdFromName(L"length", &lengthName);
ret = JsGetProperty(xnm, lengthName, &lengthValue);
int count;
JsNumberToInt(lengthValue, &count);
//wstring si [] = { L"tokyo",L"yokohama",L"oosaka" };
std::vector
for (int i = 0; i < count; i++)
{
JsValueRef index;
JsIntToNumber(i, &index);
JsValueRef IndexedProperty;
ret = JsGetIndexedProperty(xnm, index, &IndexedProperty);
JsValueRef itemref;
JsConvertValueToString(IndexedProperty, &itemref);
WCHAR* cb; UINT len;
JsStringToPointer(itemref, (const wchar_t**) &cb, &len);
si.push_back(cb); // tokyo,yokohama,oosaka
}
wcout << L'{';
for (int i = 0; i < count; i++)
{
JsValueRef index;
JsPointerToString(si[i].c_str(), si[i].length(), &index);
JsValueRef IndexedProperty;
JsGetIndexedProperty(jsobj, index, &IndexedProperty );
JsValueRef itemref;
JsConvertValueToString( IndexedProperty, &itemref );
WCHAR* cb; UINT len;
ret = JsStringToPointer( itemref, (const wchar_t**)&cb, &len );
wcout << si[i] << ':' << cb << L',';
}
wcout << L'}' << endl;
// output is "tokyo:03,yokohama:045,oosaka:06,"
system("pause");
// Dispose runtime
JsSetCurrentContext(JS_INVALID_REFERENCE);
JsDisposeRuntime(runtime);
return 0;
}