Handle<Value> JsEngine::AnyToV8(jsvalue v, int32_t contextId) { if (v.type == JSVALUE_TYPE_EMPTY) { return Handle<Value>(); } if (v.type == JSVALUE_TYPE_NULL) { return Null(); } if (v.type == JSVALUE_TYPE_BOOLEAN) { return Boolean::New(v.value.i32); } if (v.type == JSVALUE_TYPE_INTEGER) { return Int32::New(v.value.i32); } if (v.type == JSVALUE_TYPE_NUMBER) { return Number::New(v.value.num); } if (v.type == JSVALUE_TYPE_STRING) { return String::New(v.value.str); } if (v.type == JSVALUE_TYPE_DATE) { return Date::New(v.value.num); } // Arrays are converted to JS native arrays. if (v.type == JSVALUE_TYPE_ARRAY) { Local<Array> a = Array::New(v.length); for(int i = 0; i < v.length; i++) { a->Set(i, AnyToV8(v.value.arr[i], contextId)); } return a; } // This is an ID to a managed object that lives inside the JsContext keep-alive // cache. We just wrap it and the pointer to the engine inside an External. A // managed error is still a CLR object so it is wrapped exactly as a normal // managed object. if (v.type == JSVALUE_TYPE_MANAGED || v.type == JSVALUE_TYPE_MANAGED_ERROR) { ManagedRef* ref = new ManagedRef(this, contextId, v.length); Local<Object> object = (*(managed_template_))->InstanceTemplate()->NewInstance(); if (object.IsEmpty()) { return Null(); } Persistent<Object> persistent = Persistent<Object>::New(object); persistent->SetInternalField(0, External::New(ref)); persistent.MakeWeak(NULL, managed_destroy); return persistent; } return Null(); }
bool ScriptSystem::RemoveFromComponentMap(dtEntity::ComponentType ct, dtEntity::EntityId eid) { ComponentMap::iterator it = mComponentMap.find(std::make_pair(ct, eid)); if(it == mComponentMap.end()) { return false; } HandleScope scope; Persistent<Object> obj = it->second; assert(!obj.IsEmpty() && obj->IsObject()); // invalidate component wrapper obj->SetInternalField(0, External::New(0)); obj.Dispose(); mComponentMap.erase(it); V8::AdjustAmountOfExternalAllocatedMemory(-(int)sizeof(dtEntity::Component)); return true; }
Image *Image::CreateFromFile(const char *file) { ce::Image *baseImage = ce::Image::CreateFromFile(file); if(!baseImage) return 0; Image *image = new Image(); image->m_image = baseImage; Handle<ObjectTemplate> objectTemplate = ObjectTemplate::New(); objectTemplate->SetInternalFieldCount(1); HandleScope handleScope; Persistent<Object> instance = Persistent<Object>::New(objectTemplate->NewInstance()); instance->SetInternalField(0, External::New(image)); image->m_instance = instance; return image; }