axmol/testjs/JSBindings/ScriptingCore.h

143 lines
2.9 KiB
C
Raw Normal View History

2012-04-18 03:43:08 +08:00
//
// ScriptingCore.h
// testmonkey
//
// Created by Rolando Abarca on 3/14/12.
// Copyright (c) 2012 Zynga Inc. All rights reserved.
//
#ifndef cocos2dx_ScriptingCore_h
#define cocos2dx_ScriptingCore_h
#include "jsapi.h"
#include "cocos2d.h"
class ScriptingCore
{
JSRuntime *rt;
JSContext *cx;
JSObject *global;
ScriptingCore();
public:
~ScriptingCore();
static ScriptingCore & getInstance() {
static ScriptingCore instance;
return instance;
};
/**
* will eval the specified string
* @param string The string with the javascript code to be evaluated
* @param outVal The jsval that will hold the return value of the evaluation.
* Can be NULL.
2012-04-18 03:43:08 +08:00
*/
bool evalString(const char *string, jsval *outVal);
2012-04-18 03:43:08 +08:00
/**
* will run the specified string
* @param string The path of the script to be run
*/
void runScript(const char *path);
/**
* @return the global context
*/
JSContext* getGlobalContext() {
return cx;
};
/**
* @param cx
* @param message
* @param report
*/
static void reportError(JSContext *cx, const char *message, JSErrorReport *report)
{
fprintf(stderr, "%s:%u:%s\n",
report->filename ? report->filename : "<no filename=\"filename\">",
(unsigned int) report->lineno,
message);
};
/**
* Log something using CCLog
* @param cx
* @param argc
* @param vp
*/
static JSBool log(JSContext *cx, uint32_t argc, jsval *vp)
{
if (argc > 0) {
JSString *string = NULL;
JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "S", &string);
if (string) {
char *cstr = JS_EncodeString(cx, string);
cocos2d::CCLog(cstr);
}
}
return JS_TRUE;
2012-04-27 02:04:19 +08:00
};
/**
* run a script from script :)
*/
static JSBool executeScript(JSContext *cx, uint32_t argc, jsval *vp)
{
if (argc == 1) {
JSString *string;
if (JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "S", &string) == JS_TRUE) {
ScriptingCore::getInstance().runScript(JS_EncodeString(cx, string));
}
}
return JS_TRUE;
2012-04-18 03:43:08 +08:00
};
/**
* Register an object as a member of the GC's root set, preventing
* them from being GC'ed
*/
static JSBool addRootJS(JSContext *cx, uint32_t argc, jsval *vp)
{
if (argc == 1) {
JSObject *o = NULL;
if (JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "o", &o) == JS_TRUE) {
if (JS_AddObjectRoot(cx, &o) == JS_FALSE) {
cocos2d::CCLog("something went wrong when setting an object to the root");
}
}
}
return JS_TRUE;
};
/**
* removes an object from the GC's root, allowing them to be GC'ed if no
* longer referenced.
*/
static JSBool removeRootJS(JSContext *cx, uint32_t argc, jsval *vp)
{
if (argc == 1) {
JSObject *o = NULL;
if (JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "o", &o) == JS_TRUE) {
JS_RemoveObjectRoot(cx, &o);
}
}
return JS_TRUE;
};
/**
* Force a cycle of GC
* @param cx
* @param argc
* @param vp
*/
static JSBool forceGC(JSContext *cx, uint32_t argc, jsval *vp)
{
JS_GC(cx);
return JS_TRUE;
};
};
#endif