Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Linux/MacOS create function with return sample #91

Open
haojingus opened this issue Nov 19, 2019 · 0 comments
Open

Linux/MacOS create function with return sample #91

haojingus opened this issue Nov 19, 2019 · 0 comments

Comments

@haojingus
Copy link

`//-------------------------------------------------------------------------------------------------------
// Author:HAO Jing
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------

#include "ChakraCore.h"
#include "ChakraCommon.h"
#include <stdlib.h>
#include <stdio.h>
#include
#include
#include

#define FAIL_CHECK(cmd)
do
{
JsErrorCode errCode = cmd;
if (errCode != JsNoError)
{
printf("Error %d at '%s'\n",
errCode, #cmd);
return 1;
}
} while(0)

using namespace std;
JsValueRef CHAKRA_CALLBACK JSHello(JsValueRef caller,bool isConstructCall,JsValueRef* arguments,unsigned short argumentCount,void* callbackState);
JsValueRef CHAKRA_CALLBACK JSLogCB(JsValueRef caller,bool isConstructCall,JsValueRef* arguments,unsigned short argumentCount,void* callbackState);

void setCallback(JsValueRef* object,const char* propertyName,JsNativeFunction callback,void* callbackState);
//int x =1;
int main()
{
JsRuntimeHandle runtime;
JsContextRef context;
JsValueRef result;
unsigned currentSourceContext = 0;

// Your script; try replace hello-world with something else
//const char* script = "(()=>{return \'Hello World!\';})()";
const char* script = "logCB(hello(1,2));";
// Create a runtime.
FAIL_CHECK(JsCreateRuntime(JsRuntimeAttributeNone, nullptr, &runtime));

// Create an execution context.
FAIL_CHECK(JsCreateContext(runtime, &context));


// Now set the current execution context.
FAIL_CHECK(JsSetCurrentContext(context));
JsValueRef globalObj;
JsGetGlobalObject(&globalObj);
setCallback(&globalObj, "hello", JSHello, nullptr);
setCallback(&globalObj, "logCB", JSLogCB, nullptr);
//JsSetCurrentContext(context);
JsValueRef scriptSource;
JsCreateString(script, strlen(script), &scriptSource);
//JsCreateExternalArrayBuffer((void*)script, (unsigned int)strlen(script), nullptr, nullptr, &scriptSource);
JsValueRef fname;
FAIL_CHECK(JsCreateString("sample", strlen("sample"), &fname));

JsRun(scriptSource, currentSourceContext++, fname, JsParseScriptAttributeNone, &result);

// Dispose runtime
FAIL_CHECK(JsSetCurrentContext(JS_INVALID_REFERENCE));
FAIL_CHECK(JsDisposeRuntime(runtime));
std::cout<<"host end!"<<std::endl;
return 0;

}

JsValueRef CHAKRA_CALLBACK JSLogCB(JsValueRef caller,bool isConstructCall,JsValueRef* arguments,unsigned short argumentCount,void* callbackState)
{
cout<<argumentCount<<endl;
char* buff = nullptr;
JsValueRef strMsg;
JsConvertValueToString(arguments[1], &strMsg);
size_t length;
JsCopyString(strMsg, buff, 0, &length);
buff = new char[length+1];
JsCopyString(strMsg, buff, length+1, nullptr);
cout<<buff<<endl;
return JS_INVALID_REFERENCE;
}

JsValueRef CHAKRA_CALLBACK JSHello(JsValueRef caller,bool isConstructCall,JsValueRef* arguments,unsigned short argumentCount,void* callbackState)
{
//return JS_INVALID_REFERENCE;
//JsValueRef ret = JS_INVALID_REFERENCE;
double x,y,z;
JsNumberToDouble(arguments[1], &x);
JsNumberToDouble(arguments[2], &y);
z = x+y;
JsValueRef ret;
const char* msg = "return";
JsCreateString(msg, strlen(msg), &ret);
std::cout<<"this is a native function"<<std::endl;
return ret;
//return ret;
}

void setCallback(JsValueRef* object,const char* propertyName,JsNativeFunction callback,void* callbackState)
{

JsPropertyIdRef propertyId;
JsCreatePropertyId(propertyName, strlen(propertyName), &propertyId);
JsValueRef function;
JsCreateFunction(callback, callbackState, &function);
JsSetProperty(*object, propertyId, function, true);

}

`

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
@haojingus and others