2015-09-19 00:18:28 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2015 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-09-25 22:46:10 +08:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "mozilla/Maybe.h"
|
2015-09-19 00:18:28 +08:00
|
|
|
#include "CCComponentJS.h"
|
|
|
|
#include "base/CCScriptSupport.h"
|
|
|
|
#include "ScriptingCore.h"
|
2015-10-08 09:46:50 +08:00
|
|
|
#include "cocos2d_specifics.hpp"
|
2015-09-19 00:18:28 +08:00
|
|
|
#include "js_manual_conversions.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2015-09-25 22:46:10 +08:00
|
|
|
const std::string ComponentJS::ON_ENTER = "onEnter";
|
|
|
|
const std::string ComponentJS::ON_EXIT = "onExit";
|
|
|
|
const std::string ComponentJS::UPDATE = "update";
|
|
|
|
|
|
|
|
ComponentJS* ComponentJS::create(const std::string& scriptFileName)
|
2015-09-19 00:18:28 +08:00
|
|
|
{
|
|
|
|
CC_ASSERT(!scriptFileName.empty());
|
|
|
|
|
|
|
|
auto componentJS = new(std::nothrow) ComponentJS(scriptFileName);
|
|
|
|
if (componentJS)
|
|
|
|
{
|
|
|
|
componentJS->autorelease();
|
|
|
|
}
|
|
|
|
|
|
|
|
return componentJS;
|
|
|
|
}
|
|
|
|
|
|
|
|
ComponentJS::ComponentJS(const std::string& scriptFileName)
|
2015-09-25 22:46:10 +08:00
|
|
|
: _scriptFileName(scriptFileName)
|
|
|
|
, _jsObj(nullptr)
|
2015-09-19 00:18:28 +08:00
|
|
|
{
|
|
|
|
ScriptingCore* engine = ScriptingCore::getInstance();
|
|
|
|
JSContext* cx = engine->getGlobalContext();
|
|
|
|
// Require script
|
|
|
|
JS::RootedValue classValue(cx);
|
|
|
|
_succeedLoadingScript = engine->requireScript(_scriptFileName.c_str(), &classValue);
|
|
|
|
|
|
|
|
if (_succeedLoadingScript)
|
|
|
|
{
|
2015-11-27 01:26:54 +08:00
|
|
|
JS::RootedObject classObj(cx, classValue.toObjectOrNull());
|
2015-09-22 00:41:03 +08:00
|
|
|
const JSClass* theClass = JS_GetClass(classObj);
|
|
|
|
JS::RootedValue protoValue(cx);
|
2015-11-27 01:26:54 +08:00
|
|
|
JS_GetProperty(cx, classObj, "prototype", &protoValue);
|
2015-09-22 00:41:03 +08:00
|
|
|
|
2015-09-25 22:46:10 +08:00
|
|
|
mozilla::Maybe<JS::PersistentRootedObject> *jsObj = new mozilla::Maybe<JS::PersistentRootedObject>();
|
|
|
|
|
2015-12-15 11:48:44 +08:00
|
|
|
js_type_class_t *typeClass = js_get_type_from_native<cocos2d::ComponentJS>(this);
|
2015-09-22 00:41:03 +08:00
|
|
|
JS::RootedObject proto(cx, protoValue.toObjectOrNull());
|
2015-11-27 10:16:11 +08:00
|
|
|
JS::RootedObject parent(cx, typeClass->proto.ref());
|
2015-09-25 22:46:10 +08:00
|
|
|
jsObj->construct(cx);
|
2015-11-27 14:54:25 +08:00
|
|
|
JS::RootedObject obj(cx, JS_NewObject(cx, theClass, proto, parent));
|
|
|
|
jsObj->ref() = obj;
|
2015-09-22 00:41:03 +08:00
|
|
|
|
|
|
|
// link the native object with the javascript object
|
2015-11-27 10:16:11 +08:00
|
|
|
jsb_new_proxy(this, jsObj->ref());
|
2015-09-25 22:46:10 +08:00
|
|
|
|
|
|
|
_jsObj = jsObj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ComponentJS::~ComponentJS()
|
|
|
|
{
|
|
|
|
mozilla::Maybe<JS::PersistentRootedObject>* jsObj = static_cast<mozilla::Maybe<JS::PersistentRootedObject>*>(_jsObj);
|
2015-12-15 11:48:44 +08:00
|
|
|
if (jsObj && !jsObj->empty())
|
|
|
|
{
|
|
|
|
// Remove proxy
|
2015-12-17 09:37:44 +08:00
|
|
|
js_proxy_t* proxy = jsb_get_js_proxy(jsObj->ref());
|
|
|
|
if (proxy)
|
|
|
|
jsb_remove_proxy(proxy);
|
2015-12-15 11:48:44 +08:00
|
|
|
}
|
|
|
|
// Delete rooted object
|
2015-11-16 11:50:48 +08:00
|
|
|
if (jsObj != nullptr)
|
2015-09-25 22:46:10 +08:00
|
|
|
{
|
|
|
|
delete jsObj;
|
2015-09-19 00:18:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void* ComponentJS::getScriptObject() const
|
|
|
|
{
|
2015-09-25 22:46:10 +08:00
|
|
|
mozilla::Maybe<JS::PersistentRootedObject>* jsObj = static_cast<mozilla::Maybe<JS::PersistentRootedObject>*>(_jsObj);
|
2015-12-15 11:48:44 +08:00
|
|
|
if (jsObj && !jsObj->empty())
|
|
|
|
{
|
|
|
|
return jsObj->ref().get();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-09-19 00:18:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ComponentJS::update(float delta)
|
|
|
|
{
|
|
|
|
if (_succeedLoadingScript)
|
|
|
|
{
|
2015-09-25 22:46:10 +08:00
|
|
|
mozilla::Maybe<JS::PersistentRootedObject>* jsObj = static_cast<mozilla::Maybe<JS::PersistentRootedObject>*>(_jsObj);
|
2015-09-19 00:18:28 +08:00
|
|
|
jsval dataVal = DOUBLE_TO_JSVAL(delta);
|
|
|
|
JS::RootedValue retval(ScriptingCore::getInstance()->getGlobalContext());
|
2015-09-25 22:46:10 +08:00
|
|
|
ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(jsObj->ref().get()), ComponentJS::UPDATE.c_str(), 1, &dataVal, &retval);
|
2015-09-19 00:18:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComponentJS::onEnter()
|
|
|
|
{
|
|
|
|
if (_succeedLoadingScript)
|
|
|
|
{
|
2015-09-25 22:46:10 +08:00
|
|
|
mozilla::Maybe<JS::PersistentRootedObject>* jsObj = static_cast<mozilla::Maybe<JS::PersistentRootedObject>*>(_jsObj);
|
2015-09-19 00:18:28 +08:00
|
|
|
jsval dataVal = INT_TO_JSVAL(1);
|
|
|
|
JS::RootedValue retval(ScriptingCore::getInstance()->getGlobalContext());
|
2015-09-25 22:46:10 +08:00
|
|
|
ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(jsObj->ref().get()), ComponentJS::ON_ENTER.c_str(), 1, &dataVal, &retval);
|
2015-09-19 00:18:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComponentJS::onExit()
|
|
|
|
{
|
|
|
|
if (_succeedLoadingScript)
|
|
|
|
{
|
2015-09-25 22:46:10 +08:00
|
|
|
mozilla::Maybe<JS::PersistentRootedObject>* jsObj = static_cast<mozilla::Maybe<JS::PersistentRootedObject>*>(_jsObj);
|
2015-09-19 00:18:28 +08:00
|
|
|
jsval dataVal = INT_TO_JSVAL(1);
|
|
|
|
JS::RootedValue retval(ScriptingCore::getInstance()->getGlobalContext());
|
2015-09-25 22:46:10 +08:00
|
|
|
ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(jsObj->ref().get()), ComponentJS::ON_EXIT.c_str(), 1, &dataVal, &retval);
|
2015-09-19 00:18:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|