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;
}

你可能感兴趣的:(SpiderMonkey 基础版)