SpiderMonkey 基础版

#include "jsapi.h"
#include <string>
#include<fstream>
#include<iomanip>
#include <iostream>
using namespace std;
using namespace JS;

bool jsb_applyStorePay(JSContext *cx, uint32_t argc, jsval *vp)
{
	JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
	JS::RootedObject obj(cx, args.thisv().toObjectOrNull());
	bool ok = true;
// 	js_proxy_t *proxy = jsb_get_js_proxy(obj);
// 	TinyJSWork* cobj = (TinyJSWork *)(proxy ? proxy->ptr : NULL);
// 	JSB_PRECONDITION2( cobj, cx, false, "js_TinyJSWork_TinyJSWork_isJailbrokenOrInBeiJing : Invalid Native Object");
	if (argc == 1) {
		int arg0;
		//ok &= JSVAL_TO_INT32_IMPL(cx, args.get(0), &arg0);
		
	    int ret =args.get(0).toNumber() + 1;
		jsval jsret = JSVAL_NULL;
		jsret = INT_TO_JSVAL(ret);
		args.rval().set(jsret);
		return true;
	}

	JS_ReportError(cx, "js_TinyJSWork_TinyJSWork_isJailbrokenOrInBeiJing : wrong number of arguments: %d, was expecting %d", argc, 0);
	return false;
}
void register_all_js_cpp_cb(JSContext* cx, JS::HandleObject global)
{
	JS_DefineFunction(cx, global, "applyStorePay", jsb_applyStorePay, 4, JSPROP_ENUMERATE | JSPROP_PERMANENT);
}

// The class of the global object.
static JSClass globalClass = {
	"global",
	JSCLASS_GLOBAL_FLAGS,
	JS_PropertyStub,
	JS_DeletePropertyStub,
	JS_PropertyStub,
	JS_StrictPropertyStub,
	JS_EnumerateStub,
	JS_ResolveStub,
	JS_ConvertStub,
	nullptr, nullptr, nullptr, nullptr,
	JS_GlobalObjectTraceHook
};

// The error reporter callback.
void reportError(JSContext *cx, const char *message, JSErrorReport *report) {
	fprintf(stderr, "%s:%u:%s\n",
		report->filename ? report->filename : "[no filename]",
		(unsigned int) report->lineno,
		message);
}

int run(JSContext *cx) {
	// Enter a request before running anything in the context.
	JSAutoRequest ar(cx);

	// Create the global object and a new compartment.
	RootedObject global(cx);
	global = JS_NewGlobalObject(cx, &globalClass, nullptr,
		JS::DontFireOnNewGlobalHook);
	if (!global)
		return 1;

	// Enter the new global object's compartment.
	JSAutoCompartment ac(cx, global);

	// Populate the global object with the standard globals, like Object and
	// Array.
	if (!JS_InitStandardClasses(cx, global))
		return 1;

	// Your application code here. This may include JSAPI calls to create your
	// own custom JS objects and run scripts.
	register_all_js_cpp_cb(cx, global);

// 	std::string script = "applyStorePay();";
 		//JS::RootedValue outval(cx);
		//jsval argv;
	//JS_CallFunctionName(cx, global, "applyStorePay",  JS::HandleValueArray::fromMarkedLocation(0, &argv), &outval);

		string script;
		FILE * fp;
		char str1;
		if((fp=fopen("MyJsFile.js","r"))==NULL)
		{
			printf("file cannot be opened\n");
			exit(1);
		}
		while((str1=fgetc(fp))!=EOF){
			script +=str1;
		}

		fclose(fp);

	JS_EvaluateScript(cx, JS::RootedObject(cx, global),script.c_str(),script.length(),"",0);
	return 0;
}

int main(int argc, const char *argv[]) {
	// Initialize the JS engine.
	if (!JS_Init())
		return 1;

	// Create a JS runtime.
	JSRuntime *rt = JS_NewRuntime(8L * 1024L * 1024L);
	if (!rt)
		return 1;

	// Create a context.
	JSContext *cx = JS_NewContext(rt, 8192);
	if (!cx)
		return 1;
	//JS_SetErrorReporter(rt, reportError);

	int status = run(cx);

	// Shut everything down.
	JS_DestroyContext(cx);
	JS_DestroyRuntime(rt);
	JS_ShutDown();

	return status;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值