axmol/testjs/JSBindings/ScriptingCore.cpp

176 lines
7.6 KiB
C++
Raw Normal View History

2012-04-18 03:43:08 +08:00
//
// ScriptingCore.cpp
// testmonkey
//
// Created by Rolando Abarca on 3/14/12.
// Copyright (c) 2012 Zynga Inc. All rights reserved.
//
#include <iostream>
#include "cocos2d.h"
#include "ScriptingCore.h"
#include "cocos2d_generated.hpp"
#include "cocos_denshion_generated.hpp"
2012-04-18 03:43:08 +08:00
using namespace cocos2d;
static JSClass global_class = {
"global", JSCLASS_GLOBAL_FLAGS,
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
JSCLASS_NO_OPTIONAL_MEMBERS
};
ScriptingCore::ScriptingCore()
{
this->rt = JS_NewRuntime(8 * 1024 * 1024);
this->cx = JS_NewContext(rt, 8192);
JS_SetOptions(this->cx, JSOPTION_VAROBJFIX);
JS_SetVersion(this->cx, JSVERSION_LATEST);
JS_SetErrorReporter(this->cx, ScriptingCore::reportError);
global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL);
if (!JS_InitStandardClasses(cx, global)) {
CCLog("js error");
}
// create the cocos namespace
JSObject *cocos = JS_NewObject(cx, NULL, NULL, NULL);
jsval cocosVal = OBJECT_TO_JSVAL(cocos);
JS_SetProperty(cx, global, "cocos", &cocosVal);
// register the internal classes
S_CCPoint::jsCreateClass(this->cx, cocos, "Point");
S_CCSize::jsCreateClass(this->cx, cocos, "Size");
S_CCRect::jsCreateClass(this->cx, cocos, "Rect");
2012-05-04 08:45:28 +08:00
S__ccGridSize::jsCreateClass(this->cx, cocos, "GridSize");
2012-04-27 02:04:19 +08:00
S_CCSet::jsCreateClass(this->cx, cocos, "Set");
2012-04-18 03:43:08 +08:00
S_CCTouch::jsCreateClass(this->cx, cocos, "Touch");
S_CCDirector::jsCreateClass(this->cx, cocos, "Director");
S_CCNode::jsCreateClass(this->cx, cocos, "Node");
2012-05-04 08:14:56 +08:00
S_CCTextureAtlas::jsCreateClass(this->cx, cocos, "TextureAtlas");
S_CCSpriteBatchNode::jsCreateClass(this->cx, cocos, "SpriteBatchNode");
2012-04-18 03:43:08 +08:00
S_CCScene::jsCreateClass(this->cx, cocos, "Scene");
S_CCLayer::jsCreateClass(this->cx, cocos, "Layer");
S_CCSprite::jsCreateClass(this->cx, cocos, "Sprite");
S_CCRenderTexture::jsCreateClass(this->cx, cocos, "RenderTexture");
2012-04-27 02:04:19 +08:00
S_CCMenu::jsCreateClass(this->cx, cocos, "Menu");
S_CCMenuItem::jsCreateClass(this->cx, cocos, "MenuItem");
S_CCMenuItemLabel::jsCreateClass(this->cx, cocos, "MenuItemLabel");
S_CCMenuItemSprite::jsCreateClass(this->cx, cocos, "MenuItemSprite");
2012-05-03 06:03:41 +08:00
S_CCMenuItemImage::jsCreateClass(this->cx, cocos, "MenuItemImage");
2012-04-18 03:43:08 +08:00
S_CCSpriteFrame::jsCreateClass(this->cx, cocos, "SpriteFrame");
S_CCSpriteFrameCache::jsCreateClass(this->cx, cocos, "SpriteFrameCache");
S_CCAnimation::jsCreateClass(this->cx, cocos, "Animation");
S_CCAction::jsCreateClass(this->cx, cocos, "Action");
2012-05-01 02:13:28 +08:00
S_CCActionInterval::jsCreateClass(this->cx, cocos, "ActionInterval");
S_CCFiniteTimeAction::jsCreateClass(this->cx, cocos, "FiniteTimeAction");
2012-05-04 02:59:47 +08:00
S_CCDelayTime::jsCreateClass(this->cx, cocos, "DelayTime");
2012-04-18 03:43:08 +08:00
S_CCAnimate::jsCreateClass(this->cx, cocos, "Animate");
S_CCMoveTo::jsCreateClass(this->cx, cocos, "MoveTo");
2012-05-03 06:03:41 +08:00
S_CCMoveBy::jsCreateClass(this->cx, cocos, "MoveBy");
2012-04-18 03:43:08 +08:00
S_CCRotateBy::jsCreateClass(this->cx, cocos, "RotateBy");
S_CCRotateTo::jsCreateClass(this->cx, cocos, "RotateTo");
2012-05-03 06:03:41 +08:00
S_CCActionEase::jsCreateClass(this->cx, cocos, "ActionEase");
S_CCEaseRateAction::jsCreateClass(this->cx, cocos, "EaseRateAction");
2012-05-04 00:27:26 +08:00
S_CCEaseIn::jsCreateClass(this->cx, cocos, "EaseIn");
S_CCEaseOut::jsCreateClass(this->cx, cocos, "EaseOut");
S_CCEaseInOut::jsCreateClass(this->cx, cocos, "EaseInOut");
2012-05-01 02:13:28 +08:00
S_CCEaseBackInOut::jsCreateClass(this->cx, cocos, "EaseBackInOut");
S_CCEaseBackOut::jsCreateClass(this->cx, cocos, "EaseBackOut");
S_CCEaseElasticIn::jsCreateClass(this->cx, cocos, "EaseElasticIn");
S_CCEaseElastic::jsCreateClass(this->cx, cocos, "EaseElastic");
S_CCEaseElasticOut::jsCreateClass(this->cx, cocos, "EaseElasticOut");
S_CCEaseElasticInOut::jsCreateClass(this->cx, cocos, "EaseElasticInOut");
S_CCEaseBounce::jsCreateClass(this->cx, cocos, "EaseBounce");
2012-05-04 00:27:26 +08:00
S_CCEaseBounceIn::jsCreateClass(this->cx, cocos, "EaseBounceIn");
2012-05-01 02:13:28 +08:00
S_CCEaseBounceInOut::jsCreateClass(this->cx, cocos, "EaseBounceInOut");
S_CCEaseBackIn::jsCreateClass(this->cx, cocos, "EaseBackIn");
S_CCEaseBounceOut::jsCreateClass(this->cx, cocos, "EaseBounceOut");
S_CCEaseExponentialIn::jsCreateClass(this->cx, cocos, "EaseExponentialIn");
S_CCEaseExponentialOut::jsCreateClass(this->cx, cocos, "EaseExponentialOut");
S_CCEaseExponentialInOut::jsCreateClass(this->cx, cocos, "EaseExponentialInOut");
S_CCEaseSineIn::jsCreateClass(this->cx, cocos, "EaseSineIn");
S_CCEaseSineOut::jsCreateClass(this->cx, cocos, "EaseSineOut");
S_CCEaseSineInOut::jsCreateClass(this->cx, cocos, "EaseSineInOut");
2012-04-18 03:43:08 +08:00
S_CCRepeatForever::jsCreateClass(this->cx, cocos, "RepeatForever");
2012-04-27 02:04:19 +08:00
S_CCSequence::jsCreateClass(this->cx, cocos, "Sequence");
S_CCLabelTTF::jsCreateClass(this->cx, cocos, "LabelTTF");
2012-05-01 02:13:28 +08:00
S_CCParticleSystem::jsCreateClass(this->cx, cocos, "ParticleSystem");
2012-05-02 06:19:52 +08:00
S_CCFileUtils::jsCreateClass(this->cx, cocos, "FileUtils");
2012-05-04 06:46:50 +08:00
S_CCTexture2D::jsCreateClass(this->cx, cocos, "Texture2D");
S_CCTextureCache::jsCreateClass(this->cx, cocos, "TextureCache");
2012-05-04 08:45:28 +08:00
S_CCParallaxNode::jsCreateClass(this->cx, cocos, "ParallaxNode");
S_CCTintBy::jsCreateClass(this->cx, cocos, "TintBy");
S_CCTintTo::jsCreateClass(this->cx, cocos, "TintTo");
S_CCLayerColor::jsCreateClass(this->cx, cocos, "LayerColor");
S_CCBlink::jsCreateClass(this->cx, cocos, "Blink");
S_CCSpeed::jsCreateClass(this->cx, cocos, "Speed");
S_CCGridAction::jsCreateClass(this->cx, cocos, "GridAction");
S_CCGrid3DAction::jsCreateClass(this->cx, cocos, "Grid3DAction");
S_CCWaves3D::jsCreateClass(this->cx, cocos, "Waves3D");
2012-04-18 03:43:08 +08:00
S_SimpleAudioEngine::jsCreateClass(this->cx, cocos, "SimpleAudioEngine");
2012-04-18 03:43:08 +08:00
// register some global functions
JS_DefineFunction(this->cx, cocos, "log", ScriptingCore::log, 0, JSPROP_READONLY | JSPROP_PERMANENT);
2012-04-27 02:04:19 +08:00
JS_DefineFunction(this->cx, cocos, "executeScript", ScriptingCore::executeScript, 1, JSPROP_READONLY | JSPROP_PERMANENT);
JS_DefineFunction(this->cx, cocos, "addGCRootObject", ScriptingCore::addRootJS, 1, JSPROP_READONLY | JSPROP_PERMANENT);
JS_DefineFunction(this->cx, cocos, "removeGCRootObject", ScriptingCore::removeRootJS, 1, JSPROP_READONLY | JSPROP_PERMANENT);
2012-04-18 03:43:08 +08:00
JS_DefineFunction(this->cx, cocos, "forceGC", ScriptingCore::forceGC, 0, JSPROP_READONLY | JSPROP_PERMANENT);
}
bool ScriptingCore::evalString(const char *string, jsval *outVal)
2012-04-18 03:43:08 +08:00
{
jsval rval;
JSString *str;
JSBool ok;
const char *filename = "noname";
uint32_t lineno = 0;
if (outVal == NULL) {
outVal = &rval;
}
ok = JS_EvaluateScript(cx, global, string, strlen(string), filename, lineno, outVal);
if (ok == JS_FALSE) {
2012-04-18 03:43:08 +08:00
CCLog("error evaluating script:\n%s", string);
}
str = JS_ValueToString(cx, rval);
return ok;
2012-04-18 03:43:08 +08:00
}
void ScriptingCore::runScript(const char *path)
{
2012-04-27 02:04:19 +08:00
#ifdef DEBUG
2012-04-28 02:51:16 +08:00
/**
* dpath should point to the parent directory of the "JS" folder. If this is
* set to "" (as it is now) then it will take the scripts from the app bundle.
* By setting the absolute path you can iterate the development only by
* modifying those scripts and reloading from the simulator (no recompiling/
* relaunching)
*/
// std::string dpath("/Users/rabarca/Desktop/testjs/testjs/");
std::string dpath("");
2012-04-27 02:04:19 +08:00
dpath += path;
2012-04-28 02:51:16 +08:00
const char *realPath = CCFileUtils::fullPathFromRelativePath(dpath.c_str());
2012-04-27 02:04:19 +08:00
#else
2012-04-18 03:43:08 +08:00
const char *realPath = CCFileUtils::fullPathFromRelativePath(path);
2012-04-27 02:04:19 +08:00
#endif
2012-04-18 03:43:08 +08:00
unsigned char *content = NULL;
size_t contentSize = CCFileUtils::ccLoadFileIntoMemory(realPath, &content);
if (content && contentSize) {
JSBool ok;
jsval rval;
ok = JS_EvaluateScript(this->cx, this->global, (char *)content, contentSize, path, 1, &rval);
if (ok == JS_FALSE) {
2012-04-18 03:43:08 +08:00
CCLog("error evaluating script:\n%s", content);
}
free(content);
}
}
ScriptingCore::~ScriptingCore()
{
JS_DestroyContext(cx);
JS_DestroyRuntime(rt);
JS_ShutDown();
}