cleanup for chakracore.

This commit is contained in:
James Chen
2017-08-02 18:06:40 +08:00
parent 47b39f8f2e
commit e92eb70779
17 changed files with 115 additions and 165 deletions
@@ -6,6 +6,7 @@
#include "Utils.hpp"
#include "State.hpp"
#include "ScriptEngine.hpp"
#include "../HandleObject.hpp"
namespace se {
@@ -94,13 +95,14 @@ namespace se {
}
_CHECK(JsCreateNamedFunction(funcName, _ctor, nullptr, &jsConstructor));
Object* ctorObj = Object::_createJSObject(nullptr, jsConstructor, false);
HandleObject ctorObj(Object::_createJSObject(nullptr, jsConstructor));
// create class's prototype and project its member functions
JsValueRef prototype;
_CHECK(JsCreateObject(&prototype));
Object* prototypeObj = Object::_createJSObject(this, prototype, true);
Object* prototypeObj = Object::_createJSObject(this, prototype);
prototypeObj->root();
for (const auto& func : _funcs)
{
@@ -134,8 +136,6 @@ namespace se {
_proto = prototypeObj;
_parent->setProperty(_name.c_str(), Value(ctorObj));
ctorObj->release();
return true;
}
@@ -47,12 +47,15 @@
{ \
if (nativeThisObject != nullptr) \
{ \
auto se = se::ScriptEngine::getInstance(); \
se->_setInGC(true); \
bool ret = false; \
se::State state(nativeThisObject); \
se::Object* _thisObject = state.thisObject(); \
if (_thisObject) _thisObject->_cleanup(nativeThisObject); \
ret = funcName(state); \
SAFE_RELEASE(_thisObject); \
se->_setInGC(false); \
} \
}
@@ -68,7 +71,7 @@
bool ret = true; \
se::ValueArray args; \
se::internal::jsToSeArgs(argc, _argv+1, &args); \
se::Object* thisObject = se::Object::createObjectWithClass(cls, false); \
se::Object* thisObject = se::Object::createObjectWithClass(cls); \
JsValueRef _jsRet = thisObject->_getJSObject(); \
se::State state(thisObject, args); \
ret = funcName(state); \
@@ -98,7 +101,7 @@
bool ret = true; \
se::ValueArray args; \
se::internal::jsToSeArgs(argc, _argv+1, &args); \
se::Object* thisObject = se::Object::_createJSObject(cls, _argv[0], false); \
se::Object* thisObject = se::Object::_createJSObject(cls, _argv[0]); \
thisObject->_setFinalizeCallback(_SE(finalizeCb)); \
se::State state(thisObject, args); \
ret = funcName(state); \
@@ -8,13 +8,13 @@
namespace se {
std::unordered_map<void* /*native*/, Object* /*jsobj*/> __nativePtrToObjectMap;
std::unordered_map<void* /*native*/, bool> __nonRefNativeObjectCreatedByCtorMap;
Object::Object()
: _cls(nullptr)
, _obj(JS_INVALID_REFERENCE)
, _isRooted(false)
, _isKeepRootedUntilDie(false)
, _hasPrivateData(false)
, _rootCount(0)
, _privateData(nullptr)
, _isCleanup(false)
, _finalizeCb(nullptr)
{
@@ -25,31 +25,23 @@ namespace se {
_cleanup();
}
Object* Object::createPlainObject(bool rooted)
Object* Object::createPlainObject()
{
JsValueRef jsobj;
_CHECK(JsCreateObject(&jsobj));
Object* obj = _createJSObject(nullptr, jsobj, rooted);
Object* obj = _createJSObject(nullptr, jsobj);
return obj;
}
// Object* Object::createObject(const char* clsName, bool rooted)
// {
// Class* cls = nullptr;
// JsValueRef jsObj = Class::_createJSObject(clsName, &cls);
// Object* obj = _createJSObject(cls, jsObj, rooted);
// return obj;
// }
Object* Object::createArrayObject(size_t length, bool rooted)
Object* Object::createArrayObject(size_t length)
{
JsValueRef jsObj = JS_INVALID_REFERENCE;
_CHECK(JsCreateArray((unsigned int)length, &jsObj));
Object* obj = _createJSObject(nullptr, jsObj, rooted);
Object* obj = _createJSObject(nullptr, jsObj);
return obj;
}
Object* Object::createArrayBufferObject(void* data, size_t byteLength, bool rooted)
Object* Object::createArrayBufferObject(void* data, size_t byteLength)
{
Object* obj = nullptr;
JsValueRef jsobj;
@@ -59,13 +51,13 @@ namespace se {
if (JsNoError == JsGetArrayBufferStorage(jsobj, &buffer, &bufferLength))
{
memcpy((void*)buffer, data, byteLength);
obj = Object::_createJSObject(nullptr, jsobj, rooted);
obj = Object::_createJSObject(nullptr, jsobj);
}
return obj;
}
Object* Object::createUint8TypedArray(uint8_t* data, size_t byteLength, bool rooted)
Object* Object::createUint8TypedArray(uint8_t* data, size_t byteLength)
{
Object* obj = nullptr;
JsValueRef jsobj;
@@ -77,13 +69,13 @@ namespace se {
if (JsNoError == JsGetTypedArrayStorage(jsobj, &buffer, &bufferLength, &arrType, &elementSize))
{
memcpy((void*)buffer, data, byteLength);
obj = Object::_createJSObject(nullptr, jsobj, rooted);
obj = Object::_createJSObject(nullptr, jsobj);
}
return obj;
}
Object* Object::createJSONObject(const std::string& jsonStr, bool rooted)
Object* Object::createJSONObject(const std::string& jsonStr)
{
bool ok = false;
Object* obj = nullptr;
@@ -102,7 +94,7 @@ namespace se {
args.push_back(Value(jsonStr));
if (parseVal.toObject()->call(args, jsonVal.toObject(), &ret))
{
obj = Object::_createJSObject(nullptr, ret.toObject(), rooted);
obj = Object::_createJSObject(nullptr, ret.toObject());
}
return obj;
@@ -120,21 +112,10 @@ namespace se {
return obj;
}
// Object* Object::getOrCreateObjectWithPtr(void* ptr, const char* clsName, bool rooted)
// {
// Object* obj = getObjectWithPtr(ptr);
// if (obj == nullptr)
// {
// obj = createObject(clsName, rooted);
// obj->setPrivateData(ptr);
// }
// return obj;
// }
Object* Object::_createJSObject(Class* cls, JsValueRef obj, bool rooted)
Object* Object::_createJSObject(Class* cls, JsValueRef obj)
{
Object* ret = new Object();
if (!ret->init(obj, rooted))
if (!ret->init(obj))
{
delete ret;
ret = nullptr;
@@ -144,22 +125,16 @@ namespace se {
return ret;
}
Object* Object::createObjectWithClass(Class* cls, bool rooted)
Object* Object::createObjectWithClass(Class* cls)
{
JsValueRef jsobj = Class::_createJSObjectWithClass(cls);
Object* obj = Object::_createJSObject(cls, jsobj, rooted);
Object* obj = Object::_createJSObject(cls, jsobj);
return obj;
}
bool Object::init(JsValueRef obj, bool rooted)
bool Object::init(JsValueRef obj)
{
_obj = obj;
_isRooted = rooted;
if (_isRooted)
{
unsigned int count = 0;
_CHECK(JsAddRef(_obj, &count));
}
return true;
}
@@ -168,7 +143,7 @@ namespace se {
if (_isCleanup)
return;
if (_hasPrivateData)
if (_privateData != nullptr)
{
if (_obj != nullptr)
{
@@ -188,7 +163,7 @@ namespace se {
}
}
if (_isRooted)
if (_rootCount > 0)
{
// Don't unprotect if it's in cleanup, otherwise, it will trigger crash.
if (!ScriptEngine::getInstance()->_isInCleanup)
@@ -196,6 +171,7 @@ namespace se {
unsigned int count = 0;
_CHECK(JsRelease(_obj, &count));
}
_rootCount = 0;
}
_isCleanup = true;
@@ -205,6 +181,7 @@ namespace se {
{
ScriptEngine::getInstance()->addAfterCleanupHook([](){
__nativePtrToObjectMap.clear();
__nonRefNativeObjectCreatedByCtorMap.clear();
});
}
@@ -533,48 +510,32 @@ namespace se {
return ret;
}
// void Object::getAsUint8Array(unsigned char **ptr, unsigned int *length)
// {
// assert(false);
// }
//
// void Object::getAsUint16Array(unsigned short **ptr, unsigned int *length)
// {
// assert(false);
// }
// void Object::getAsUint32Array(unsigned int **ptr, unsigned int *length)
// {
// assert(false);
// }
//
// void Object::getAsFloat32Array(float **ptr, unsigned int *length)
// {
// assert(false);
// }
void* Object::getPrivateData()
void* Object::getPrivateData() const
{
return internal::getPrivate(_obj);
if (_privateData == nullptr)
{
const_cast<Object*>(this)->_privateData = internal::getPrivate(_obj);
}
return _privateData;
}
void Object::setPrivateData(void *data)
void Object::setPrivateData(void* data)
{
assert(!_hasPrivateData);
assert(_privateData == nullptr);
assert(__nativePtrToObjectMap.find(data) == __nativePtrToObjectMap.end());
internal::setPrivate(_obj, data, _finalizeCb);
__nativePtrToObjectMap.emplace(data, this);
_hasPrivateData = true;
_privateData = data;
}
void Object::clearPrivateData()
{
if (_hasPrivateData)
if (_privateData != nullptr)
{
void* data = getPrivateData();
__nativePtrToObjectMap.erase(data);
internal::clearPrivate(_obj);
_hasPrivateData = false;
_privateData = nullptr;
}
}
@@ -590,41 +551,30 @@ namespace se {
void Object::root()
{
if (_isRooted)
return;
unsigned int count = 0;
_CHECK(JsAddRef(_obj, &count));
_isRooted = true;
if (_rootCount == 0)
{
unsigned int count = 0;
_CHECK(JsAddRef(_obj, &count));
}
++_rootCount;
}
void Object::unroot()
{
if (!_isRooted)
return;
if (_isKeepRootedUntilDie)
return;
unsigned int count = 0;
_CHECK(JsRelease(_obj, &count));
_isRooted = false;
}
void Object::setKeepRootedUntilDie(bool keepRooted)
{
_isKeepRootedUntilDie = keepRooted;
if (_isKeepRootedUntilDie)
if (_rootCount > 0)
{
if (!_isRooted)
root();
--_rootCount;
if (_rootCount == 0)
{
unsigned int count = 0;
_CHECK(JsRelease(_obj, &count));
}
}
}
bool Object::isRooted() const
{
return _isRooted;
return _rootCount > 0;
}
bool Object::isSame(Object* o) const
@@ -14,35 +14,20 @@ namespace se {
class Object final : public Ref
{
private:
Object();
public:
virtual ~Object();
static Object* createPlainObject(bool rooted);
// static Object* createObject(const char* clsName, bool rooted);
static Object* createArrayObject(size_t length, bool rooted);
static Object* createUint8TypedArray(uint8_t* data, size_t byteLength, bool rooted);
static Object* createArrayBufferObject(void* data, size_t byteLength, bool rooted);
static Object* createJSONObject(const std::string& jsonStr, bool rooted);
static Object* createPlainObject();
static Object* createArrayObject(size_t length);
static Object* createUint8TypedArray(uint8_t* data, size_t byteLength);
static Object* createArrayBufferObject(void* data, size_t byteLength);
static Object* createJSONObject(const std::string& jsonStr);
static Object* getObjectWithPtr(void* ptr);
// static Object* getOrCreateObjectWithPtr(void* ptr, const char* clsName, bool rooted);
static Object* createObjectWithClass(Class* cls, bool rooted);
static Object* _createJSObject(Class* cls, JsValueRef obj, bool rooted);
bool init(JsValueRef obj, bool rooted);
static Object* createObjectWithClass(Class* cls);
bool getProperty(const char* name, Value* data);
void setProperty(const char* name, const Value& v);
bool defineProperty(const char *name, JsNativeFunction getter, JsNativeFunction setter);
JsValueRef _getJSObject() const;
Class* _getClass() const;
void _cleanup(void* nativeObject = nullptr);
void _setFinalizeCallback(JsFinalizeCallback finalizeCb);
bool isFunction() const;
bool _isNativeFunction() const;
bool call(const ValueArray& args, Object* thisObject, Value* rval = nullptr);
bool defineFunction(const char *funcName, JsNativeFunction func);
@@ -50,11 +35,6 @@ namespace se {
bool isTypedArray() const;
bool getTypedArrayData(uint8_t** ptr, size_t* length) const;
// void getAsUint8Array(unsigned char **ptr, unsigned int *length);
// void getAsUint16Array(unsigned short **ptr, unsigned int *length);
// void getAsUint32Array(unsigned int **ptr, unsigned int *length);
// void getAsFloat32Array(float **ptr, unsigned int *length);
bool isArray() const;
bool getArrayLength(uint32_t* length) const;
bool getArrayElement(uint32_t index, Value* data) const;
@@ -66,26 +46,37 @@ namespace se {
bool getAllKeys(std::vector<std::string>* allKeys) const;
void setPrivateData(void* data);
void* getPrivateData();
void* getPrivateData() const;
void clearPrivateData();
void root();
void unroot();
void setKeepRootedUntilDie(bool keepRooted);
bool isRooted() const;
bool isSame(Object* o) const;
bool attachChild(Object* child);
bool detachChild(Object* child);
// Private API used in wrapper
static Object* _createJSObject(Class* cls, JsValueRef obj);
JsValueRef _getJSObject() const;
Class* _getClass() const;
void _cleanup(void* nativeObject = nullptr);
void _setFinalizeCallback(JsFinalizeCallback finalizeCb);
bool _isNativeFunction() const;
//
private:
static void cleanup();
Object();
virtual ~Object();
bool init(JsValueRef obj);
Class* _cls;
JsValueRef _obj;
bool _isRooted;
bool _isKeepRootedUntilDie;
bool _hasPrivateData;
uint32_t _rootCount;
void* _privateData;
bool _isCleanup;
JsFinalizeCallback _finalizeCb;
@@ -94,6 +85,7 @@ namespace se {
extern std::unordered_map<void* /*native*/, Object* /*jsobj*/> __nativePtrToObjectMap;
extern std::unordered_map<void* /*native*/, bool> __nonRefNativeObjectCreatedByCtorMap;
} // namespace se {
@@ -76,6 +76,7 @@ namespace se {
, _globalObj(nullptr)
, _isValid(false)
, _isInCleanup(false)
, _isInGC(false)
, _currentSourceContext(0)
{
}
@@ -96,7 +97,8 @@ namespace se {
_CHECK(JsSetRuntimeBeforeCollectCallback(_rt, nullptr, myJsBeforeCollectCallback));
_globalObj = Object::_createJSObject(nullptr, globalObj, true);
_globalObj = Object::_createJSObject(nullptr, globalObj);
_globalObj->root();
_globalObj->defineFunction("log", __log);
_globalObj->defineFunction("forceGC", __forceGC);
@@ -242,6 +244,16 @@ namespace se {
return ok;
}
bool ScriptEngine::isInGC()
{
return _isInGC;
}
void ScriptEngine::_setInGC(bool isInGC)
{
_isInGC = isInGC;
}
void ScriptEngine::gc()
{
LOGD("GC begin ..., (Native -> JS map) count: %d\n", (int)__nativePtrToObjectMap.size());
@@ -46,6 +46,8 @@ namespace se {
bool executeScriptBuffer(const char *string, Value *data = nullptr, const char *fileName = nullptr);
bool executeScriptBuffer(const char *string, size_t length, Value *data = nullptr, const char *fileName = nullptr);
bool isInGC();
void _setInGC(bool isInGC);
void gc();
bool isValid() { return _isValid; }
@@ -78,6 +80,7 @@ namespace se {
bool _isValid;
bool _isInCleanup;
bool _isInGC;
unsigned _currentSourceContext;
NodeEventListener _nodeEventListener;
@@ -12,12 +12,10 @@
#include "Object.hpp"
#include "ScriptEngine.hpp"
#include "../HandleObject.hpp"
namespace se {
const bool NEED_THIS = true;
const bool DONT_NEED_THIS = false;
namespace internal {
bool defineProperty(JsValueRef obj, const char* name, JsNativeFunction getter, JsNativeFunction setter, bool enumerable, bool configurable)
@@ -117,7 +115,18 @@ namespace se {
else if (type == JsObject || type == JsFunction || type == JsArrayBuffer
|| type == JsTypedArray || type == JsArray)
{
Object* obj = Object::_createJSObject(nullptr, jsValue, true);
void* nativePtr = internal::getPrivate(jsValue);
Object* obj = nullptr;
if (nativePtr != nullptr)
{
obj = Object::getObjectWithPtr(nativePtr);
}
if (obj == nullptr)
{
obj = Object::_createJSObject(nullptr, jsValue);
obj->root();
}
data->setObject(obj);
obj->release();
}
@@ -216,7 +225,7 @@ namespace se {
}
assert(finalizeCb);
Object* privateObj = Object::createObjectWithClass(__jsb_CCPrivateData_class, false);
HandleObject privateObj(Object::createObjectWithClass(__jsb_CCPrivateData_class));
internal::PrivateData* privateData = (internal::PrivateData*)malloc(sizeof(internal::PrivateData));
privateData->data = data;
privateData->finalizeCb = finalizeCb;
@@ -226,7 +235,6 @@ namespace se {
JsPropertyIdRef propertyId = JS_INVALID_REFERENCE;
JsCreatePropertyId(KEY_PRIVATE_DATA, strlen(KEY_PRIVATE_DATA), &propertyId);
_CHECK(JsSetProperty(obj, propertyId, privateObj->_getJSObject(), true));
privateObj->release();
}
void* getPrivate(JsValueRef obj)
@@ -10,9 +10,6 @@
namespace se {
extern const bool NEED_THIS;
extern const bool DONT_NEED_THIS;
namespace internal {
struct PrivateData
@@ -174,6 +174,8 @@ namespace se {
// Don't unprotect if it's in cleanup, otherwise, it will trigger crash.
if (!ScriptEngine::getInstance()->_isInCleanup)
JSValueUnprotect(__cx, _obj);
_rootCount = 0;
}
_isCleanup = true;
@@ -22,7 +22,6 @@ namespace se {
static Object* createJSONObject(const std::string& jsonStr);
static Object* getObjectWithPtr(void* ptr);
static Object* createObjectWithClass(Class* cls);
static Object* _createJSObject(Class* cls, JSObjectRef obj);
bool getProperty(const char* name, Value* data);
void setProperty(const char* name, const Value& v);
@@ -60,6 +59,7 @@ namespace se {
bool detachChild(Object* child);
// Private API used in wrapper
static Object* _createJSObject(Class* cls, JSObjectRef obj);
JSObjectRef _getJSObject() const;
Class* _getClass() const;
void _setFinalizeCallback(JSObjectFinalizeCallback finalizeCb);
@@ -12,13 +12,10 @@
#include "Object.hpp"
#include "ScriptEngine.hpp"
#include "HandleObject.hpp"
#include "../HandleObject.hpp"
namespace se {
const bool NEED_THIS = true;
const bool DONT_NEED_THIS = false;
namespace internal {
namespace {
@@ -9,9 +9,6 @@
#include "../Value.hpp"
namespace se {
extern const bool NEED_THIS;
extern const bool DONT_NEED_THIS;
namespace internal {
@@ -16,9 +16,6 @@
namespace se {
const bool NEED_THIS = true;
const bool DONT_NEED_THIS = false;
namespace internal {
std::string jsToStdString(JSContext* cx, JS::HandleString jsStr)
@@ -10,9 +10,6 @@
namespace se {
extern const bool NEED_THIS;
extern const bool DONT_NEED_THIS;
class Class;
namespace internal {
@@ -27,7 +27,7 @@ namespace se {
static Object* createJSONObject(const std::string& jsonStr);
static Object* getObjectWithPtr(void* ptr);
static Object* createObjectWithClass(Class* cls);
static Object* _createJSObject(Class* cls, v8::Local<v8::Object> obj);
bool getProperty(const char *name, Value* data);
void setProperty(const char *name, const Value& data);
@@ -65,6 +65,7 @@ namespace se {
bool detachChild(Object* child);
// Private API used in wrapper
static Object* _createJSObject(Class* cls, v8::Local<v8::Object> obj);
v8::Local<v8::Object> _getJSObject() const;
ObjectWrap& _getWrap();
Class* _getClass() const;
@@ -8,9 +8,6 @@
namespace se {
const bool NEED_THIS = true;
const bool DONT_NEED_THIS = false;
namespace internal {
void jsToSeArgs(const v8::FunctionCallbackInfo<v8::Value>& v8args, ValueArray* outArr)
@@ -9,9 +9,6 @@
#include "ObjectWrap.h"
namespace se {
extern const bool NEED_THIS;
extern const bool DONT_NEED_THIS;
namespace internal {