mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
cleanup, and final class.
This commit is contained in:
@@ -6,7 +6,7 @@ namespace se {
|
||||
|
||||
class Object;
|
||||
|
||||
class State
|
||||
class State final
|
||||
{
|
||||
public:
|
||||
State();
|
||||
|
||||
@@ -10,115 +10,136 @@ namespace se {
|
||||
|
||||
Value::Value()
|
||||
: _type(Type::Undefined)
|
||||
, _autoRootUnroot(false)
|
||||
{
|
||||
memset(&_u, 0, sizeof(_u));
|
||||
}
|
||||
|
||||
Value::Value(Type type)
|
||||
: _type(Type::Undefined)
|
||||
, _autoRootUnroot(false)
|
||||
{
|
||||
memset(&_u, 0, sizeof(_u));
|
||||
reset(type);
|
||||
}
|
||||
|
||||
Value::Value(const Value& v)
|
||||
: _type(Type::Undefined)
|
||||
, _autoRootUnroot(false)
|
||||
{
|
||||
*this = v;
|
||||
}
|
||||
|
||||
Value::Value(Value&& v)
|
||||
: _type(Type::Undefined)
|
||||
, _autoRootUnroot(false)
|
||||
{
|
||||
*this = std::move(v);
|
||||
}
|
||||
|
||||
Value::Value(bool v)
|
||||
: _type(Type::Undefined)
|
||||
, _autoRootUnroot(false)
|
||||
{
|
||||
setBoolean(v);
|
||||
}
|
||||
|
||||
Value::Value(int8_t v)
|
||||
: _type(Type::Undefined)
|
||||
, _autoRootUnroot(false)
|
||||
{
|
||||
setInt8(v);
|
||||
}
|
||||
|
||||
Value::Value(uint8_t v)
|
||||
: _type(Type::Undefined)
|
||||
, _autoRootUnroot(false)
|
||||
{
|
||||
setUint8(v);
|
||||
}
|
||||
|
||||
Value::Value(int32_t v)
|
||||
: _type(Type::Undefined)
|
||||
, _autoRootUnroot(false)
|
||||
{
|
||||
setInt32(v);
|
||||
}
|
||||
|
||||
Value::Value(uint32_t v)
|
||||
: _type(Type::Undefined)
|
||||
, _autoRootUnroot(false)
|
||||
{
|
||||
setUint32(v);
|
||||
}
|
||||
|
||||
Value::Value(int16_t v)
|
||||
: _type(Type::Undefined)
|
||||
, _autoRootUnroot(false)
|
||||
{
|
||||
setInt16(v);
|
||||
}
|
||||
|
||||
Value::Value(uint16_t v)
|
||||
: _type(Type::Undefined)
|
||||
, _autoRootUnroot(false)
|
||||
{
|
||||
setUint16(v);
|
||||
}
|
||||
|
||||
Value::Value(long v)
|
||||
: _type(Type::Undefined)
|
||||
, _autoRootUnroot(false)
|
||||
{
|
||||
setLong(v);
|
||||
}
|
||||
|
||||
Value::Value(unsigned long v)
|
||||
: _type(Type::Undefined)
|
||||
, _autoRootUnroot(false)
|
||||
{
|
||||
setUlong(v);
|
||||
}
|
||||
|
||||
Value::Value(float v)
|
||||
: _type(Type::Undefined)
|
||||
, _autoRootUnroot(false)
|
||||
{
|
||||
setFloat(v);
|
||||
}
|
||||
|
||||
Value::Value(double v)
|
||||
: _type(Type::Undefined)
|
||||
, _autoRootUnroot(false)
|
||||
{
|
||||
setNumber(v);
|
||||
}
|
||||
|
||||
Value::Value(const char* v)
|
||||
: _type(Type::Undefined)
|
||||
, _autoRootUnroot(false)
|
||||
{
|
||||
setString(v);
|
||||
}
|
||||
|
||||
Value::Value(const std::string& v)
|
||||
: _type(Type::Undefined)
|
||||
, _autoRootUnroot(false)
|
||||
{
|
||||
setString(v);
|
||||
}
|
||||
|
||||
Value::Value(Object* o)
|
||||
Value::Value(Object* o, bool autoRootUnroot/* = false*/)
|
||||
: _type(Type::Undefined)
|
||||
, _autoRootUnroot(false)
|
||||
{
|
||||
setObject(o);
|
||||
setObject(o, autoRootUnroot);
|
||||
}
|
||||
|
||||
Value::Value(const HandleObject& o)
|
||||
Value::Value(const HandleObject& o, bool autoRootUnroot/* = false*/)
|
||||
: _type(Type::Undefined)
|
||||
, _autoRootUnroot(false)
|
||||
{
|
||||
setObject(o);
|
||||
setObject(o, autoRootUnroot);
|
||||
}
|
||||
|
||||
Value::~Value()
|
||||
@@ -134,7 +155,11 @@ namespace se {
|
||||
|
||||
switch (_type) {
|
||||
case Type::Null:
|
||||
case Type::Undefined:
|
||||
{
|
||||
memset(&_u, 0, sizeof(_u));
|
||||
break;
|
||||
}
|
||||
case Type::Number:
|
||||
_u._number = v._u._number;
|
||||
break;
|
||||
@@ -146,7 +171,7 @@ namespace se {
|
||||
break;
|
||||
case Type::Object:
|
||||
{
|
||||
setObject(v._u._object);
|
||||
setObject(v._u._object, v._autoRootUnroot);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@@ -164,7 +189,11 @@ namespace se {
|
||||
|
||||
switch (_type) {
|
||||
case Type::Null:
|
||||
case Type::Undefined:
|
||||
{
|
||||
memset(&_u, 0, sizeof(_u));
|
||||
break;
|
||||
}
|
||||
case Type::Number:
|
||||
_u._number = v._u._number;
|
||||
break;
|
||||
@@ -176,12 +205,18 @@ namespace se {
|
||||
break;
|
||||
case Type::Object:
|
||||
{
|
||||
if (_u._object != nullptr)
|
||||
if (_u._object != nullptr) // When old value is also an Object, reset will take no effect, therefore, _u._object may not be nullptr.
|
||||
{
|
||||
if (_autoRootUnroot)
|
||||
{
|
||||
_u._object->unroot();
|
||||
}
|
||||
_u._object->release();
|
||||
}
|
||||
_u._object = v._u._object;
|
||||
v._u._object = nullptr;
|
||||
_autoRootUnroot = v._autoRootUnroot;
|
||||
v._u._object = nullptr; // Reset to nullptr here to avoid 'release' operation in v.reset(Type::Undefined) since it's a move operation here.
|
||||
v._autoRootUnroot = false;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@@ -193,35 +228,35 @@ namespace se {
|
||||
return *this;
|
||||
}
|
||||
|
||||
Value& Value::operator=(bool v)
|
||||
{
|
||||
setBoolean(v);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Value& Value::operator=(double v)
|
||||
{
|
||||
setNumber(v);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Value& Value::operator=(const std::string& v)
|
||||
{
|
||||
setString(v);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Value& Value::operator=(Object* o)
|
||||
{
|
||||
setObject(o);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Value& Value::operator=(const HandleObject& o)
|
||||
{
|
||||
setObject(o);
|
||||
return *this;
|
||||
}
|
||||
// Value& Value::operator=(bool v)
|
||||
// {
|
||||
// setBoolean(v);
|
||||
// return *this;
|
||||
// }
|
||||
//
|
||||
// Value& Value::operator=(double v)
|
||||
// {
|
||||
// setNumber(v);
|
||||
// return *this;
|
||||
// }
|
||||
//
|
||||
// Value& Value::operator=(const std::string& v)
|
||||
// {
|
||||
// setString(v);
|
||||
// return *this;
|
||||
// }
|
||||
//
|
||||
// Value& Value::operator=(Object* o)
|
||||
// {
|
||||
// setObject(o);
|
||||
// return *this;
|
||||
// }
|
||||
//
|
||||
// Value& Value::operator=(const HandleObject& o)
|
||||
// {
|
||||
// setObject(o);
|
||||
// return *this;
|
||||
// }
|
||||
|
||||
void Value::setUndefined()
|
||||
{
|
||||
@@ -318,30 +353,54 @@ namespace se {
|
||||
*_u._string = v;
|
||||
}
|
||||
|
||||
void Value::setObject(Object* object)
|
||||
void Value::setObject(Object* object, bool autoRootUnroot/* = false*/)
|
||||
{
|
||||
if (object != nullptr)
|
||||
reset(Type::Object);
|
||||
else
|
||||
if (object == nullptr)
|
||||
{
|
||||
reset(Type::Null);
|
||||
return;
|
||||
}
|
||||
|
||||
if (_type != Type::Object)
|
||||
{
|
||||
reset(Type::Object);
|
||||
}
|
||||
|
||||
if (_u._object != object)
|
||||
{
|
||||
if (_u._object != nullptr)
|
||||
if (object != nullptr)
|
||||
{
|
||||
object->addRef();
|
||||
if (autoRootUnroot)
|
||||
{
|
||||
object->root();
|
||||
}
|
||||
}
|
||||
|
||||
if (_u._object != nullptr) // When old value is also an Object, reset will take no effect, therefore, _u._object may not be nullptr.
|
||||
{
|
||||
if (_autoRootUnroot)
|
||||
{
|
||||
_u._object->unroot();
|
||||
}
|
||||
_u._object->release();
|
||||
}
|
||||
_u._object = object;
|
||||
if (_u._object != nullptr)
|
||||
_autoRootUnroot = autoRootUnroot;
|
||||
}
|
||||
else
|
||||
{
|
||||
_autoRootUnroot = autoRootUnroot;
|
||||
if (_autoRootUnroot)
|
||||
{
|
||||
_u._object->addRef();
|
||||
_u._object->root();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Value::setObject(const HandleObject& o)
|
||||
void Value::setObject(const HandleObject& o, bool autoRootUnroot/* = false*/)
|
||||
{
|
||||
setObject(o.get());
|
||||
setObject(o.get(), autoRootUnroot);
|
||||
}
|
||||
|
||||
int8_t Value::toInt8() const
|
||||
@@ -469,8 +528,20 @@ namespace se {
|
||||
delete _u._string;
|
||||
break;
|
||||
case Type::Object:
|
||||
SAFE_RELEASE(_u._object);
|
||||
{
|
||||
if (_u._object != nullptr)
|
||||
{
|
||||
if (_autoRootUnroot)
|
||||
{
|
||||
_u._object->unroot();
|
||||
}
|
||||
_u._object->release();
|
||||
_u._object = nullptr;
|
||||
}
|
||||
|
||||
_autoRootUnroot = false;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace se {
|
||||
|
||||
class Object;
|
||||
|
||||
class Value
|
||||
class Value final
|
||||
{
|
||||
public:
|
||||
enum class Type
|
||||
@@ -42,19 +42,19 @@ namespace se {
|
||||
explicit Value(double v);
|
||||
explicit Value(const char* v);
|
||||
explicit Value(const std::string& v);
|
||||
explicit Value(Object* o);
|
||||
explicit Value(const HandleObject& o);
|
||||
explicit Value(Object* o, bool autoRootUnroot = false);
|
||||
explicit Value(const HandleObject& o, bool autoRootUnroot = false);
|
||||
|
||||
~Value();
|
||||
|
||||
Value& operator=(const Value& v);
|
||||
Value& operator=(Value&& v);
|
||||
Value& operator=(bool v);
|
||||
Value& operator=(double v);
|
||||
Value& operator=(const char* v);
|
||||
Value& operator=(const std::string& v);
|
||||
Value& operator=(Object* o);
|
||||
Value& operator=(const HandleObject& o);
|
||||
// Value& operator=(bool v);
|
||||
// Value& operator=(double v);
|
||||
// Value& operator=(const char* v);
|
||||
// Value& operator=(const std::string& v);
|
||||
// Value& operator=(Object* o);
|
||||
// Value& operator=(const HandleObject& o);
|
||||
|
||||
void setUndefined();
|
||||
void setNull();
|
||||
@@ -71,8 +71,8 @@ namespace se {
|
||||
void setNumber(double v);
|
||||
void setString(const char* v);
|
||||
void setString(const std::string& v);
|
||||
void setObject(Object* o);
|
||||
void setObject(const HandleObject& o);
|
||||
void setObject(Object* o, bool autoRootUnroot = false);
|
||||
void setObject(const HandleObject& o, bool autoRootUnroot = false);
|
||||
|
||||
int8_t toInt8() const;
|
||||
uint8_t toUint8() const;
|
||||
@@ -112,6 +112,8 @@ namespace se {
|
||||
std::string* _string;
|
||||
Object* _object;
|
||||
} _u;
|
||||
|
||||
bool _autoRootUnroot;
|
||||
};
|
||||
|
||||
using ValueArray = std::vector<Value>;
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace se {
|
||||
|
||||
class Object;
|
||||
|
||||
class Class
|
||||
class Class final
|
||||
{
|
||||
private:
|
||||
Class();
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
se::internal::seToJsValue(state.rval(), &_jsRet); \
|
||||
for (auto& v : args) \
|
||||
{ \
|
||||
if (v.isObject() && v.toObject()->isRooted()) \
|
||||
if (v.isObject()) \
|
||||
{ \
|
||||
v.toObject()->unroot(); \
|
||||
} \
|
||||
@@ -81,7 +81,7 @@
|
||||
} \
|
||||
for (auto& v : args) \
|
||||
{ \
|
||||
if (v.isObject() && v.toObject()->isRooted()) \
|
||||
if (v.isObject()) \
|
||||
{ \
|
||||
v.toObject()->unroot(); \
|
||||
} \
|
||||
@@ -111,7 +111,7 @@
|
||||
} \
|
||||
for (auto& v : args) \
|
||||
{ \
|
||||
if (v.isObject() && v.toObject()->isRooted()) \
|
||||
if (v.isObject()) \
|
||||
{ \
|
||||
v.toObject()->unroot(); \
|
||||
} \
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace se {
|
||||
|
||||
class Class;
|
||||
|
||||
class Object : public Ref
|
||||
class Object final : public Ref
|
||||
{
|
||||
private:
|
||||
Object();
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace se {
|
||||
~AutoHandleScope() {}
|
||||
};
|
||||
|
||||
class ScriptEngine
|
||||
class ScriptEngine final
|
||||
{
|
||||
private:
|
||||
ScriptEngine();
|
||||
|
||||
@@ -109,11 +109,18 @@ namespace se {
|
||||
ScriptEngine::getInstance()->getGlobalObject()->getProperty("Function", &functionCtor);
|
||||
ctorObj->setProperty("constructor", functionCtor);
|
||||
|
||||
JSValueRef exception = nullptr;
|
||||
for (const auto& staticfunc : _staticFuncs)
|
||||
{
|
||||
JSStringRef name = JSStringCreateWithUTF8CString(staticfunc.name);
|
||||
JSObjectRef func = JSObjectMakeFunctionWithCallback(__cx, nullptr, staticfunc.callAsFunction);
|
||||
JSObjectSetProperty(__cx, jsCtor, name, func, kJSPropertyAttributeNone, nullptr);
|
||||
|
||||
exception = nullptr;
|
||||
JSObjectSetProperty(__cx, jsCtor, name, func, kJSPropertyAttributeNone, &exception);
|
||||
if (exception != nullptr)
|
||||
{
|
||||
ScriptEngine::getInstance()->_clearException(exception);
|
||||
}
|
||||
JSStringRelease(name);
|
||||
}
|
||||
|
||||
@@ -122,12 +129,24 @@ namespace se {
|
||||
bool exist = JSObjectHasProperty(__cx, jsCtor, prototypeName);
|
||||
if (exist)
|
||||
{
|
||||
prototypeObj = JSObjectGetProperty(__cx, jsCtor, prototypeName, nullptr);
|
||||
exception = nullptr;
|
||||
prototypeObj = JSObjectGetProperty(__cx, jsCtor, prototypeName, &exception);
|
||||
if (exception != nullptr)
|
||||
{
|
||||
ScriptEngine::getInstance()->_clearException(exception);
|
||||
}
|
||||
}
|
||||
JSStringRelease(prototypeName);
|
||||
assert(prototypeObj != nullptr);
|
||||
|
||||
_proto = Object::_createJSObject(this, JSValueToObject(__cx, prototypeObj, nullptr));
|
||||
exception = nullptr;
|
||||
JSObjectRef protoJSObj = JSValueToObject(__cx, prototypeObj, &exception);
|
||||
if (exception != nullptr)
|
||||
{
|
||||
ScriptEngine::getInstance()->_clearException(exception);
|
||||
}
|
||||
|
||||
_proto = Object::_createJSObject(this, protoJSObj);
|
||||
_proto->root();
|
||||
|
||||
// reset constructor
|
||||
@@ -184,21 +203,6 @@ namespace se {
|
||||
return true;
|
||||
}
|
||||
|
||||
// JSObjectRef Class::_createJSObject(const std::string &clsName, Class** outCls)
|
||||
// {
|
||||
// auto iter = __clsMap.find(clsName);
|
||||
// if (iter == __clsMap.end())
|
||||
// {
|
||||
// *outCls = nullptr;
|
||||
// return nullptr;
|
||||
// }
|
||||
//
|
||||
// Class* thiz = iter->second;
|
||||
// *outCls = thiz;
|
||||
//
|
||||
// return _createJSObjectWithClass(thiz);
|
||||
// }
|
||||
|
||||
JSObjectRef Class::_createJSObjectWithClass(Class* cls)
|
||||
{
|
||||
return JSObjectMake(__cx, cls->_jsCls, nullptr);
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace se {
|
||||
|
||||
class Object;
|
||||
|
||||
class Class
|
||||
class Class final
|
||||
{
|
||||
private:
|
||||
Class();
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
se::internal::seToJsValue(_cx, state.rval(), &_jsRet); \
|
||||
for (auto& v : args) \
|
||||
{ \
|
||||
if (v.isObject() && v.toObject()->isRooted()) \
|
||||
if (v.isObject()) \
|
||||
{ \
|
||||
v.toObject()->unroot(); \
|
||||
} \
|
||||
@@ -53,8 +53,8 @@
|
||||
se::State state(nativeThisObject); \
|
||||
se::Object* _thisObject = state.thisObject(); \
|
||||
if (_thisObject) _thisObject->_cleanup(nativeThisObject); \
|
||||
JSObjectSetPrivate(_obj, nullptr); \
|
||||
ret = funcName(state); \
|
||||
JSObjectSetPrivate(_obj, nullptr); \
|
||||
SAFE_RELEASE(_thisObject); \
|
||||
} \
|
||||
se->_setInGC(false); \
|
||||
@@ -84,7 +84,7 @@
|
||||
} \
|
||||
for (auto& v : args) \
|
||||
{ \
|
||||
if (v.isObject() && v.toObject()->isRooted()) \
|
||||
if (v.isObject()) \
|
||||
{ \
|
||||
v.toObject()->unroot(); \
|
||||
} \
|
||||
@@ -113,7 +113,7 @@
|
||||
} \
|
||||
for (auto& v : args) \
|
||||
{ \
|
||||
if (v.isObject() && v.toObject()->isRooted()) \
|
||||
if (v.isObject()) \
|
||||
{ \
|
||||
v.toObject()->unroot(); \
|
||||
} \
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
namespace se {
|
||||
|
||||
std::unordered_map<void* /*native*/, Object* /*jsobj*/> __nativePtrToObjectMap;
|
||||
std::unordered_map<void* /*native*/, bool> __nonRefNativeObjectCreatedByCtorMap;
|
||||
|
||||
namespace {
|
||||
JSContextRef __cx = nullptr;
|
||||
@@ -37,7 +38,13 @@ namespace se {
|
||||
|
||||
Object* Object::createArrayObject(size_t length)
|
||||
{
|
||||
JSObjectRef jsObj = JSObjectMakeArray(__cx, 0, nullptr, nullptr);
|
||||
JSValueRef exception = nullptr;
|
||||
JSObjectRef jsObj = JSObjectMakeArray(__cx, 0, nullptr, &exception);
|
||||
if (exception != nullptr)
|
||||
{
|
||||
ScriptEngine::getInstance()->_clearException(exception);
|
||||
return nullptr;
|
||||
}
|
||||
Object* obj = _createJSObject(nullptr, jsObj);
|
||||
return obj;
|
||||
}
|
||||
@@ -51,7 +58,13 @@ namespace se {
|
||||
{
|
||||
void* copiedData = malloc(byteLength);
|
||||
memcpy(copiedData, data, byteLength);
|
||||
JSObjectRef jsobj = JSObjectMakeArrayBufferWithBytesNoCopy(__cx, copiedData, byteLength, myJSTypedArrayBytesDeallocator, nullptr, nullptr);
|
||||
JSValueRef exception = nullptr;
|
||||
JSObjectRef jsobj = JSObjectMakeArrayBufferWithBytesNoCopy(__cx, copiedData, byteLength, myJSTypedArrayBytesDeallocator, nullptr, &exception);
|
||||
if (exception != nullptr)
|
||||
{
|
||||
ScriptEngine::getInstance()->_clearException(exception);
|
||||
return nullptr;
|
||||
}
|
||||
Object* obj = Object::_createJSObject(nullptr, jsobj);
|
||||
return obj;
|
||||
}
|
||||
@@ -60,7 +73,13 @@ namespace se {
|
||||
{
|
||||
void* copiedData = malloc(byteLength);
|
||||
memcpy(copiedData, data, byteLength);
|
||||
JSObjectRef jsobj = JSObjectMakeTypedArrayWithBytesNoCopy(__cx, kJSTypedArrayTypeUint8Array, copiedData, byteLength, myJSTypedArrayBytesDeallocator, nullptr, nullptr);
|
||||
JSValueRef exception = nullptr;
|
||||
JSObjectRef jsobj = JSObjectMakeTypedArrayWithBytesNoCopy(__cx, kJSTypedArrayTypeUint8Array, copiedData, byteLength, myJSTypedArrayBytesDeallocator, nullptr, &exception);
|
||||
if (exception != nullptr)
|
||||
{
|
||||
ScriptEngine::getInstance()->_clearException(exception);
|
||||
return nullptr;
|
||||
}
|
||||
Object* obj = Object::_createJSObject(nullptr, jsobj);
|
||||
return obj;
|
||||
}
|
||||
@@ -73,7 +92,14 @@ namespace se {
|
||||
|
||||
if (ret != nullptr)
|
||||
{
|
||||
obj = Object::_createJSObject(nullptr, JSValueToObject(__cx, ret, nullptr));
|
||||
JSValueRef exception = nullptr;
|
||||
JSObjectRef jsobj = JSValueToObject(__cx, ret, &exception);
|
||||
if (exception != nullptr)
|
||||
{
|
||||
ScriptEngine::getInstance()->_clearException(exception);
|
||||
return nullptr;
|
||||
}
|
||||
obj = Object::_createJSObject(nullptr, jsobj);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
@@ -166,7 +192,13 @@ namespace se {
|
||||
|
||||
if (exist)
|
||||
{
|
||||
JSValueRef jsValue = JSObjectGetProperty(__cx, _obj, jsName, nullptr);
|
||||
JSValueRef exception = nullptr;
|
||||
JSValueRef jsValue = JSObjectGetProperty(__cx, _obj, jsName, &exception);
|
||||
if (exception != nullptr)
|
||||
{
|
||||
ScriptEngine::getInstance()->_clearException(exception);
|
||||
return false;
|
||||
}
|
||||
internal::jsToSeValue(__cx, jsValue, data);
|
||||
}
|
||||
|
||||
@@ -207,7 +239,14 @@ namespace se {
|
||||
jsValue = JSValueMakeUndefined(__cx);
|
||||
}
|
||||
|
||||
JSObjectSetProperty(__cx, obj, jsName, jsValue, kJSPropertyAttributeNone, nullptr);
|
||||
JSValueRef exception = nullptr;
|
||||
JSObjectSetProperty(__cx, obj, jsName, jsValue, kJSPropertyAttributeNone, &exception);
|
||||
|
||||
if (exception != nullptr)
|
||||
{
|
||||
ScriptEngine::getInstance()->_clearException(exception);
|
||||
}
|
||||
|
||||
JSStringRelease(jsName);
|
||||
}
|
||||
|
||||
@@ -250,12 +289,7 @@ namespace se {
|
||||
// Function call failed, try to output exception
|
||||
if (exception != nullptr)
|
||||
{
|
||||
std::string exceptionStr = se::ScriptEngine::getInstance()->_formatException(exception);
|
||||
if (!exceptionStr.empty())
|
||||
{
|
||||
LOGD("%s\n", exceptionStr.c_str());
|
||||
}
|
||||
se::ScriptEngine::getInstance()->clearException();
|
||||
ScriptEngine::getInstance()->_clearException(exception);
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -264,8 +298,13 @@ namespace se {
|
||||
bool Object::defineFunction(const char* funcName, JSObjectCallAsFunctionCallback func)
|
||||
{
|
||||
JSStringRef jsName = JSStringCreateWithUTF8CString(funcName);
|
||||
JSObjectRef jsFunc = JSObjectMakeFunctionWithCallback(__cx, nullptr, func);
|
||||
JSObjectSetProperty(__cx, _obj, jsName, jsFunc, kJSPropertyAttributeNone, nullptr);
|
||||
JSObjectRef jsFunc = JSObjectMakeFunctionWithCallback(__cx, jsName, func);
|
||||
JSValueRef exception = nullptr;
|
||||
JSObjectSetProperty(__cx, _obj, jsName, jsFunc, kJSPropertyAttributeNone, &exception);
|
||||
if (exception != nullptr)
|
||||
{
|
||||
ScriptEngine::getInstance()->_clearException(exception);
|
||||
}
|
||||
JSStringRelease(jsName);
|
||||
return true;
|
||||
}
|
||||
@@ -275,7 +314,16 @@ namespace se {
|
||||
assert(isArray());
|
||||
assert(length != nullptr);
|
||||
JSStringRef key = JSStringCreateWithUTF8CString("length");
|
||||
JSValueRef v = JSObjectGetProperty(__cx, _obj, key, nullptr);
|
||||
JSValueRef exception = nullptr;
|
||||
JSValueRef v = JSObjectGetProperty(__cx, _obj, key, &exception);
|
||||
if (exception != nullptr)
|
||||
{
|
||||
ScriptEngine::getInstance()->_clearException(exception);
|
||||
JSStringRelease(key);
|
||||
*length = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
assert(JSValueIsNumber(__cx, v));
|
||||
double len = JSValueToNumber(__cx, v, nullptr);
|
||||
JSStringRelease(key);
|
||||
@@ -288,7 +336,15 @@ namespace se {
|
||||
{
|
||||
assert(isArray());
|
||||
assert(data != nullptr);
|
||||
JSValueRef v = JSObjectGetPropertyAtIndex(__cx, _obj, index, nullptr);
|
||||
JSValueRef exception = nullptr;
|
||||
JSValueRef v = JSObjectGetPropertyAtIndex(__cx, _obj, index, &exception);
|
||||
|
||||
if (exception != nullptr)
|
||||
{
|
||||
ScriptEngine::getInstance()->_clearException(exception);
|
||||
return false;
|
||||
}
|
||||
|
||||
internal::jsToSeValue(__cx, v, data);
|
||||
|
||||
return true;
|
||||
@@ -300,7 +356,14 @@ namespace se {
|
||||
|
||||
JSValueRef v;
|
||||
internal::seToJsValue(__cx, data, &v);
|
||||
JSObjectSetPropertyAtIndex(__cx, _obj, index, v, nullptr);
|
||||
JSValueRef exception = nullptr;
|
||||
JSObjectSetPropertyAtIndex(__cx, _obj, index, v, &exception);
|
||||
if (exception != nullptr)
|
||||
{
|
||||
ScriptEngine::getInstance()->_clearException(exception);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -335,8 +398,21 @@ namespace se {
|
||||
bool Object::getTypedArrayData(uint8_t** ptr, size_t* length) const
|
||||
{
|
||||
assert(isTypedArray());
|
||||
*length = JSObjectGetTypedArrayByteLength(__cx, _obj, nullptr);
|
||||
*ptr = (uint8_t*)JSObjectGetTypedArrayBytesPtr(__cx, _obj, nullptr);
|
||||
JSValueRef exception = nullptr;
|
||||
*length = JSObjectGetTypedArrayByteLength(__cx, _obj, &exception);
|
||||
if (exception != nullptr)
|
||||
{
|
||||
ScriptEngine::getInstance()->_clearException(exception);
|
||||
return false;
|
||||
}
|
||||
|
||||
*ptr = (uint8_t*)JSObjectGetTypedArrayBytesPtr(__cx, _obj, &exception);
|
||||
if (exception != nullptr)
|
||||
{
|
||||
ScriptEngine::getInstance()->_clearException(exception);
|
||||
return false;
|
||||
}
|
||||
|
||||
return (*ptr != nullptr);
|
||||
}
|
||||
|
||||
@@ -361,7 +437,13 @@ namespace se {
|
||||
|
||||
bool Object::isArrayBuffer() const
|
||||
{
|
||||
JSTypedArrayType type = JSValueGetTypedArrayType(__cx, _obj, nullptr);
|
||||
JSValueRef exception = nullptr;
|
||||
JSTypedArrayType type = JSValueGetTypedArrayType(__cx, _obj, &exception);
|
||||
if (exception != nullptr)
|
||||
{
|
||||
ScriptEngine::getInstance()->_clearException(exception);
|
||||
return false;
|
||||
}
|
||||
return type == kJSTypedArrayTypeArrayBuffer;
|
||||
}
|
||||
|
||||
@@ -369,8 +451,21 @@ namespace se {
|
||||
{
|
||||
assert(ptr && length);
|
||||
assert(isArrayBuffer());
|
||||
*length = JSObjectGetArrayBufferByteLength(__cx, _obj, nullptr);
|
||||
*ptr = (uint8_t*)JSObjectGetArrayBufferBytesPtr(__cx, _obj, nullptr);
|
||||
JSValueRef exception = nullptr;
|
||||
*length = JSObjectGetArrayBufferByteLength(__cx, _obj, &exception);
|
||||
if (exception != nullptr)
|
||||
{
|
||||
ScriptEngine::getInstance()->_clearException(exception);
|
||||
return false;
|
||||
}
|
||||
|
||||
*ptr = (uint8_t*)JSObjectGetArrayBufferBytesPtr(__cx, _obj, &exception);
|
||||
if (exception != nullptr)
|
||||
{
|
||||
ScriptEngine::getInstance()->_clearException(exception);
|
||||
return false;
|
||||
}
|
||||
|
||||
return (*ptr != nullptr);
|
||||
}
|
||||
|
||||
@@ -408,6 +503,7 @@ namespace se {
|
||||
{
|
||||
ScriptEngine::getInstance()->addAfterCleanupHook([](){
|
||||
__nativePtrToObjectMap.clear();
|
||||
__nonRefNativeObjectCreatedByCtorMap.clear();
|
||||
__cx = nullptr;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace se {
|
||||
|
||||
class Class;
|
||||
|
||||
class Object : public Ref
|
||||
class Object final : public Ref
|
||||
{
|
||||
private:
|
||||
Object();
|
||||
@@ -87,6 +87,7 @@ namespace se {
|
||||
};
|
||||
|
||||
extern std::unordered_map<void* /*native*/, Object* /*jsobj*/> __nativePtrToObjectMap;
|
||||
extern std::unordered_map<void* /*native*/, bool> __nonRefNativeObjectCreatedByCtorMap;
|
||||
|
||||
} // namespace se {
|
||||
|
||||
|
||||
@@ -211,6 +211,18 @@ namespace se {
|
||||
return ret;
|
||||
}
|
||||
|
||||
void ScriptEngine::_clearException(JSValueRef exception)
|
||||
{
|
||||
if (exception != nullptr)
|
||||
{
|
||||
std::string exceptionStr = _formatException(exception);
|
||||
if (!exceptionStr.empty())
|
||||
{
|
||||
LOGD("%s\n", exceptionStr.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool ScriptEngine::isInGC()
|
||||
{
|
||||
return _isInGC;
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace se {
|
||||
~AutoHandleScope();
|
||||
};
|
||||
|
||||
class ScriptEngine
|
||||
class ScriptEngine final
|
||||
{
|
||||
private:
|
||||
ScriptEngine();
|
||||
@@ -73,6 +73,7 @@ namespace se {
|
||||
bool _setNodeEventListener(NodeEventListener listener);
|
||||
|
||||
std::string _formatException(JSValueRef exception);
|
||||
void _clearException(JSValueRef exception);
|
||||
|
||||
JSContextRef _getContext() const { return _cx; }
|
||||
private:
|
||||
|
||||
@@ -120,7 +120,16 @@ namespace se {
|
||||
}
|
||||
else if (JSValueIsNumber(cx, jsValue))
|
||||
{
|
||||
data->setNumber(JSValueToNumber(cx, jsValue, nullptr));
|
||||
JSValueRef exception = nullptr;
|
||||
double number = JSValueToNumber(cx, jsValue, &exception);
|
||||
if (exception != nullptr)
|
||||
{
|
||||
ScriptEngine::getInstance()->_clearException(exception);
|
||||
}
|
||||
else
|
||||
{
|
||||
data->setNumber(number);
|
||||
}
|
||||
}
|
||||
else if (JSValueIsBoolean(cx, jsValue))
|
||||
{
|
||||
@@ -134,9 +143,18 @@ namespace se {
|
||||
}
|
||||
else if (JSValueIsObject(cx, jsValue))
|
||||
{
|
||||
HandleObject obj(Object::_createJSObject(nullptr, JSValueToObject(cx, jsValue, nullptr)));
|
||||
obj->root();
|
||||
data->setObject(obj);
|
||||
JSValueRef exception = nullptr;
|
||||
JSObjectRef jsobj = JSValueToObject(cx, jsValue, &exception);
|
||||
if (exception != nullptr)
|
||||
{
|
||||
ScriptEngine::getInstance()->_clearException(exception);
|
||||
}
|
||||
else
|
||||
{
|
||||
HandleObject obj(Object::_createJSObject(nullptr, jsobj));
|
||||
obj->root();
|
||||
data->setObject(obj);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -179,8 +197,17 @@ namespace se {
|
||||
|
||||
void forceConvertJsValueToStdString(JSContextRef cx, JSValueRef jsval, std::string* ret)
|
||||
{
|
||||
JSStringRef jsstr = JSValueToStringCopy(cx, jsval, nullptr);
|
||||
jsStringToStdString(cx, jsstr, ret);
|
||||
JSValueRef exception = nullptr;
|
||||
JSStringRef jsstr = JSValueToStringCopy(cx, jsval, &exception);
|
||||
if (exception != nullptr)
|
||||
{
|
||||
ScriptEngine::getInstance()->_clearException(exception);
|
||||
ret->clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
jsStringToStdString(cx, jsstr, ret);
|
||||
}
|
||||
JSStringRelease(jsstr);
|
||||
}
|
||||
|
||||
@@ -227,7 +254,12 @@ namespace se {
|
||||
assert(ok);
|
||||
|
||||
JSStringRef key = JSStringCreateWithUTF8CString(KEY_PRIVATE_DATA);
|
||||
JSObjectSetProperty(__cx, obj, key, privateObj->_getJSObject(), kJSPropertyAttributeDontEnum, nullptr);
|
||||
JSValueRef exception = nullptr;
|
||||
JSObjectSetProperty(__cx, obj, key, privateObj->_getJSObject(), kJSPropertyAttributeDontEnum, &exception);
|
||||
if (exception != nullptr)
|
||||
{
|
||||
ScriptEngine::getInstance()->_clearException(exception);
|
||||
}
|
||||
JSStringRelease(key);
|
||||
}
|
||||
|
||||
@@ -241,10 +273,26 @@ namespace se {
|
||||
bool found = JSObjectHasProperty(__cx, obj, key);
|
||||
if (found)
|
||||
{
|
||||
JSValueRef privateDataVal = JSObjectGetProperty(__cx, obj, key, nullptr);
|
||||
internal::PrivateData* privateData = (internal::PrivateData*)JSObjectGetPrivate(JSValueToObject(__cx, privateDataVal, nullptr));
|
||||
assert(privateData != nullptr);
|
||||
data = privateData->data;
|
||||
JSValueRef exception = nullptr;
|
||||
JSValueRef privateDataVal = JSObjectGetProperty(__cx, obj, key, &exception);
|
||||
do
|
||||
{
|
||||
if (exception != nullptr)
|
||||
break;
|
||||
|
||||
JSObjectRef jsobj = JSValueToObject(__cx, privateDataVal, &exception);
|
||||
if (exception != nullptr)
|
||||
break;
|
||||
|
||||
internal::PrivateData* privateData = (internal::PrivateData*)JSObjectGetPrivate(jsobj);
|
||||
assert(privateData != nullptr);
|
||||
data = privateData->data;
|
||||
} while(false);
|
||||
|
||||
if (exception != nullptr)
|
||||
{
|
||||
ScriptEngine::getInstance()->_clearException(exception);
|
||||
}
|
||||
}
|
||||
JSStringRelease(key);
|
||||
return data;
|
||||
@@ -262,13 +310,28 @@ namespace se {
|
||||
JSStringRef key = JSStringCreateWithUTF8CString(KEY_PRIVATE_DATA); //FIXME: cache the key string
|
||||
if (JSObjectHasProperty(__cx, obj, key))
|
||||
{
|
||||
JSValueRef value = JSObjectGetProperty(__cx, obj, key, nullptr);
|
||||
JSObjectRef propertyObj = JSValueToObject(__cx, value, nullptr);
|
||||
internal::PrivateData* privateData = (internal::PrivateData*)JSObjectGetPrivate(propertyObj);
|
||||
free(privateData);
|
||||
JSObjectSetPrivate(propertyObj, nullptr);
|
||||
bool ok = JSObjectDeleteProperty(__cx, obj, key, nullptr);
|
||||
assert(ok);
|
||||
JSValueRef exception = nullptr;
|
||||
do
|
||||
{
|
||||
JSValueRef value = JSObjectGetProperty(__cx, obj, key, &exception);
|
||||
if (exception != nullptr)
|
||||
break;
|
||||
|
||||
JSObjectRef propertyObj = JSValueToObject(__cx, value, &exception);
|
||||
if (exception != nullptr)
|
||||
break;
|
||||
|
||||
internal::PrivateData* privateData = (internal::PrivateData*)JSObjectGetPrivate(propertyObj);
|
||||
free(privateData);
|
||||
JSObjectSetPrivate(propertyObj, nullptr);
|
||||
bool ok = JSObjectDeleteProperty(__cx, obj, key, nullptr);
|
||||
assert(ok);
|
||||
} while (false);
|
||||
|
||||
if (exception != nullptr)
|
||||
{
|
||||
ScriptEngine::getInstance()->_clearException(exception);
|
||||
}
|
||||
}
|
||||
|
||||
JSStringRelease(key);
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace se {
|
||||
|
||||
class Object;
|
||||
|
||||
class Class
|
||||
class Class final
|
||||
{
|
||||
private:
|
||||
Class();
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
se::internal::setReturnValue(_cx, state.rval(), _argv); \
|
||||
for (auto& v : args) \
|
||||
{ \
|
||||
if (v.isObject() && v.toObject()->isRooted()) \
|
||||
if (v.isObject()) \
|
||||
{ \
|
||||
v.toObject()->unroot(); \
|
||||
} \
|
||||
@@ -71,7 +71,7 @@
|
||||
} \
|
||||
for (auto& v : args) \
|
||||
{ \
|
||||
if (v.isObject() && v.toObject()->isRooted()) \
|
||||
if (v.isObject()) \
|
||||
{ \
|
||||
v.toObject()->unroot(); \
|
||||
} \
|
||||
@@ -101,7 +101,7 @@
|
||||
} \
|
||||
for (auto& v : args) \
|
||||
{ \
|
||||
if (v.isObject() && v.toObject()->isRooted()) \
|
||||
if (v.isObject()) \
|
||||
{ \
|
||||
v.toObject()->unroot(); \
|
||||
} \
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace se {
|
||||
|
||||
class Class;
|
||||
|
||||
class Object : public Ref
|
||||
class Object final : public Ref
|
||||
{
|
||||
private:
|
||||
Object();
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace se {
|
||||
~AutoHandleScope();
|
||||
};
|
||||
|
||||
class ScriptEngine
|
||||
class ScriptEngine final
|
||||
{
|
||||
private:
|
||||
ScriptEngine();
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace se {
|
||||
|
||||
class Object;
|
||||
|
||||
class Class
|
||||
class Class final
|
||||
{
|
||||
private:
|
||||
Class();
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
se::internal::setReturnValue(state.rval(), _v8args); \
|
||||
for (auto& v : args) \
|
||||
{ \
|
||||
if (v.isObject() && v.toObject()->isRooted()) \
|
||||
if (v.isObject()) \
|
||||
{ \
|
||||
v.toObject()->unroot(); \
|
||||
} \
|
||||
@@ -73,7 +73,7 @@
|
||||
if (_found) _property.toObject()->call(args, thisObject); \
|
||||
for (auto& v : args) \
|
||||
{ \
|
||||
if (v.isObject() && v.toObject()->isRooted()) \
|
||||
if (v.isObject()) \
|
||||
{ \
|
||||
v.toObject()->unroot(); \
|
||||
} \
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace se {
|
||||
struct PrivateData;
|
||||
}
|
||||
|
||||
class Object : public Ref
|
||||
class Object final : public Ref
|
||||
{
|
||||
private:
|
||||
Object();
|
||||
|
||||
@@ -47,7 +47,7 @@ namespace se {
|
||||
v8::HandleScope _handleScope;
|
||||
};
|
||||
|
||||
class ScriptEngine
|
||||
class ScriptEngine final
|
||||
{
|
||||
private:
|
||||
ScriptEngine();
|
||||
|
||||
Reference in New Issue
Block a user