Ejemplo n.º 1
0
int Conv::BindToJavaObject(JNIEnv *jniEnv, jobject jLocal, Handle<Object> val, Handle<String> key) {
  jobject ob;
  int result = BindToJavaObject(jniEnv, jLocal, val, &ob);
  if(result == OK)
    val->SetHiddenValue(key, External::Wrap(ob));
  return result;
}
Ejemplo n.º 2
0
 Handle< Value > ScopedThreadInit( V8Scope* scope, const Arguments &args ) {
     Handle<v8::Object> it = args.This();
     // NOTE I believe the passed JSThreadConfig will never be freed.  If this
     // policy is changed, JSThread may no longer be able to store JSThreadConfig
     // by reference.
     it->SetHiddenValue( v8::String::New( "_JSThreadConfig" ), External::New( new JSThreadConfig( scope, args, true ) ) );
     return v8::Undefined();
 }
void MetadataNode::InterfaceConstructorCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
{
	SET_PROFILER_FRAME();

	auto thiz = info.This();
	auto node = reinterpret_cast<MetadataNode*>(info.Data().As<External>()->Value());

	Handle<Object> implementationObject;
	Handle<String> v8ExtendName;
	string extendLocation;
	bool extendLocationFound = GetExtendLocation(extendLocation);
	if (info.Length() == 1)
	{
		if (!extendLocationFound)
		{
			ASSERT_FAIL("Invalid extend() call. No name specified for extend. Location: %s", extendLocation.c_str());
		}

		ASSERT_MESSAGE(info[0]->IsObject(), "Invalid extend() call. No implementation object specified. Location: %s", extendLocation.c_str());
		implementationObject = info[0]->ToObject();
	}
	else if (info.Length() == 2)
	{
		ASSERT_MESSAGE(info[0]->IsString(), "Invalid extend() call. No name for extend specified. Location: %s", extendLocation.c_str());
		ASSERT_MESSAGE(info[1]->IsObject(), "Invalid extend() call. Named extend should be called with second object parameter containing overridden methods. Location: %s", extendLocation.c_str());

		DEBUG_WRITE("InterfaceConstructorCallback: getting extend name");
		v8ExtendName = info[0]->ToString();
		implementationObject = info[1]->ToObject();
	}
	else
	{
		ASSERT_FAIL("Invalid extend() call. Location: %s", extendLocation.c_str());
	}

	auto className = node->m_implType;
	auto extendName = ConvertToString(v8ExtendName);
	auto extendNameAndLocation = extendLocation + extendName;
	SetInstanceMetadata(info.GetIsolate(), implementationObject, node);

	//@@@ Refactor
	string fullClassName = CreateFullClassName(className, extendNameAndLocation);
	thiz->SetHiddenValue(ConvertToV8String("implClassName"), ConvertToV8String(fullClassName));
	//

	jclass generatedClass = s_resolveClass(fullClassName, implementationObject);
	implementationObject->SetHiddenValue(ConvertToV8String(fullClassName), External::New(Isolate::GetCurrent(), generatedClass));//

	implementationObject->SetPrototype(thiz->GetPrototype());
	thiz->SetPrototype(implementationObject);
	thiz->SetHiddenValue(ConvertToV8String("t::implObj"), implementationObject);

	ArgsWrapper argWrapper(info, ArgType::Interface, Handle<Object>());

	auto success = s_registerInstance(thiz, fullClassName, argWrapper, implementationObject, true);

	assert(success);
}
Ejemplo n.º 4
0
void TiObject::setTiObjectToJsObject(Handle<Value> jsObject, TiObject* tiObj)
{
    if (!jsObject->IsObject())
    {
        return;
    }
    Handle<Object> obj = Handle<Object>::Cast(jsObject);
    obj->SetHiddenValue(String::New(HIDDEN_TI_OBJECT_PROPERTY), External::New(tiObj));
}
Ejemplo n.º 5
0
int Conv::BindToV8Object(JNIEnv *jniEnv, Handle<Object> val, Handle<String> key, jobject jLocal, jobject *jHard) {
  if(!jLocal || jniEnv->ExceptionCheck()) {
    jniEnv->ExceptionClear();
    return ErrorVM;
  }
  jobject jWeak = jniEnv->NewWeakGlobalRef(jLocal);
  val->SetHiddenValue(key, External::Wrap(jWeak));
  *jHard = jLocal;
  return OK;
}
void TiProxy::onAddEventListener(const char* eventName, Handle<Function> eventFunction)
{
    NativeObject* no = getNativeObject();
    if (no == NULL)
    {
        // To support events we need to create a native proxy
        // instance here if no native object has been set yet.
        no = new NativeProxyObject(this);
        setNativeObject(no);
    }
    Handle<Object> source = Handle<Object>::Cast(getValue());
    TiV8Event* event = TiV8Event::createEvent(eventName, eventFunction, source);
    no->setEventHandler(eventName, event);
    no->release();
    no = NULL;
    int id = event->getId();
    eventFunction->SetHiddenValue(String::New("POINTER"), External::New(event));
    eventFunction->SetHiddenValue(String::New("OWNER"), External::New(this));
    eventFunction->SetHiddenValue(String::New("ID"), Integer::New(id));
}
Ejemplo n.º 7
0
int Conv::BindToJavaObject(JNIEnv *jniEnv, jobject jLocal, Handle<Object> val, Interface *interface) {
  jobject ob;
  int result = BindToJavaObject(jniEnv, jLocal, val, &ob);
  if(result == OK) {
    Handle<Value> vOb = External::Wrap(ob);
    while(interface != 0) {
      val->SetHiddenValue(interface->getHiddenKey(), vOb);
      interface = interface->getParent();
    }
  }
  return result;
}
Ejemplo n.º 8
0
void Shape::New(const v8::FunctionCallbackInfo<Value>& args)
{
  HandleScope scope;
  Handle<Object> self = args.Holder();
  Shape *shape;

  if (args[0]->IsExternal())
  {
    Local<External> ext = Local<External>::Cast(args[0]);
    void *ptr = ext->Value();
    shape = static_cast<Shape*>(ptr);
    shape->Wrap(args.Holder());
  }
  else
  {
    shapeObj *s = (shapeObj *)msSmallMalloc(sizeof(shapeObj));

    msInitShape(s);
    if(args.Length() >= 1) {
      s->type = args[0]->Int32Value();
    }
    else {
      s->type = MS_SHAPE_NULL;
    }

    shape = new Shape(s);
    shape->Wrap(self);
  }

  /* create the attribute template. should use ObjectWrap in future */
  Handle<ObjectTemplate> attributes_templ = ObjectTemplate::New();
  attributes_templ->SetInternalFieldCount(2);
  attributes_templ->SetNamedPropertyHandler(attributeGetValue,
                                            attributeSetValue);
  Handle<Object> attributes = attributes_templ->NewInstance();
  map<string, int> *attributes_map = new map<string, int>();
  attributes->SetInternalField(0, External::New(attributes_map));
  attributes->SetInternalField(1, External::New(shape->get()->values));  
  attributes->SetHiddenValue(String::New("__parent__"), self);

  if (shape->layer) {
    for (int i=0; i<shape->layer->numitems; ++i) {
      (*attributes_map)[string(shape->layer->items[i])] = i;
    }
  }

  Persistent<Object> pattributes;
  pattributes.Reset(Isolate::GetCurrent(), attributes);
  pattributes.MakeWeak(attributes_map, attributeWeakCallback);
  pattributes.MarkIndependent();

  self->Set(String::New("attributes"), attributes);
}
Ejemplo n.º 9
0
	Handle<Value> CreateExternalArrayBuffer(int32_t length, uint8_t *data) {
	  Handle<Object> buffer = Object::New();
	  buffer->SetHiddenValue(String::New(kArrayBufferMarkerPropName), True());
	  Persistent<Object> persistent_array = Persistent<Object>::New(buffer);
	  persistent_array.MakeWeak(data, ExternalArrayWeakCallback);
	  persistent_array.MarkIndependent();

	  buffer->SetIndexedPropertiesToExternalArrayData(
		  data, v8::kExternalByteArray, length);
	  buffer->Set(String::New("byteLength"), Int32::New(length), ReadOnly);
	  buffer->Set(String::New("address"), Int32::New(reinterpret_cast<uint32_t>(data)), ReadOnly);

	  return buffer;
	}
Handle<Object> MetadataNode::CreateExtendedJSWrapper(Isolate *isolate, const string& proxyClassName)
{
	Handle<Object> extInstance;

	auto cacheData = GetCachedExtendedClassData(isolate, proxyClassName);
	if (cacheData.node != nullptr)
	{
		extInstance = Object::New(isolate);
		auto extdCtorFunc = Local<Function>::New(isolate, *cacheData.extendedCtorFunction);
		extInstance->SetPrototype(extdCtorFunc->Get(ConvertToV8String("prototype")));

		SetInstanceMetadata(isolate, extInstance, cacheData.node);
		extInstance->SetHiddenValue(ConvertToV8String("implClassName"), ConvertToV8String(cacheData.extendedName));
	}

	return extInstance;
}
Ejemplo n.º 11
0
Handle<Value> Layer::New(OGRLayer *raw, OGRDataSource *raw_parent, bool result_set)
#endif
{
	NanEscapableScope();

	if (!raw) {
		return NanEscapeScope(NanNull());
	}
	if (cache.has(raw)) {
		return NanEscapeScope(NanNew(cache.get(raw)));
	}

	Layer *wrapped = new Layer(raw);
	wrapped->is_result_set = result_set;

	Handle<Value> ext = NanNew<External>(wrapped);
	Handle<Object> obj = NanNew(Layer::constructor)->GetFunction()->NewInstance(1, &ext);

	cache.add(raw, obj);

	//add reference to datasource so datasource doesnt get GC'ed while layer is alive
	if (raw_parent) {
		Handle<Value> ds;
		#if GDAL_VERSION_MAJOR >= 2
			if (Dataset::dataset_cache.has(raw_parent)) {
				ds = NanNew(Dataset::dataset_cache.get(raw_parent));
			}
		#else
			if (Dataset::datasource_cache.has(raw_parent)) {
				ds = NanNew(Dataset::datasource_cache.get(raw_parent));
			}
		#endif
		else {
			LOG("Layer's parent dataset disappeared from cache (layer = %p, dataset = %p)", raw, raw_parent);
			NanThrowError("Layer's parent dataset disappeared from cache");
			return NanEscapeScope(NanUndefined());
			//ds = Dataset::New(raw_parent); //should never happen
		}

		wrapped->parent_ds = raw_parent;
		obj->SetHiddenValue(NanNew("ds_"), ds);
	}

	return NanEscapeScope(obj);
}
Ejemplo n.º 12
0
VALUE rr_reflect_v8_object_as(Handle<Value> value, VALUE ruby_class) {
  Handle<Object> object = Handle<Object>::Cast(value);
  VALUE handle;
  v8_weakref* backref;
  Local<Value> holder = object->GetHiddenValue(String::NewSymbol("TheRubyRacer::Backref"));
  if (holder.IsEmpty()) {
    handle = rr_v8_handle_new(ruby_class, object);
    backref = new v8_weakref(handle);
    object->SetHiddenValue(String::NewSymbol("TheRubyRacer::Backref"), backref->external);
  } else {
    backref = (v8_weakref*)External::Unwrap(holder);
    handle = backref->get();
    if (!RTEST(handle)) {
      handle = rr_v8_handle_new(ruby_class, object);
      backref->set(handle);
    }
  }
  return handle;
}
Ejemplo n.º 13
0
bool tns::V8SetHiddenValue(const Handle<Object>& obj, const string& propName, const Handle<Value>& value)
{
	auto s = tns::ConvertToV8String(propName);
	return obj->SetHiddenValue(s, value);
}
void MetadataNode::ExtendCallMethodHandler(const v8::FunctionCallbackInfo<v8::Value>& info)
{
	if (info.IsConstructCall())
	{
		string exMsg("Cannot call 'extend' as constructor");
		ExceptionUtil::GetInstance()->ThrowExceptionToJs(exMsg);
		return;
	}

	SET_PROFILER_FRAME();

	Handle<Object> implementationObject;
	Handle<String> extendName;
	string extendLocation;
	auto validArgs = ValidateExtendArguments(info, extendLocation, extendName, implementationObject);

	if (!validArgs)
		return;

	auto node = reinterpret_cast<MetadataNode*>(info.Data().As<External>()->Value());

	DEBUG_WRITE("ExtendsCallMethodHandler: called with %s", ConvertToString(extendName).c_str());

	string extendNameAndLocation = extendLocation + ConvertToString(extendName);
	auto fullClassName = TNS_PREFIX + CreateFullClassName(node->m_name, extendNameAndLocation);


	//
	JEnv env;
	//resolve class (pre-generated or generated runtime from dex generator)
	jclass generatedClass = s_resolveClass(fullClassName, implementationObject); //resolve class returns GlobalRef
	std::string generatedFullClassName = s_objectManager->GetClassName(generatedClass);
	//

	auto fullExtendedName = generatedFullClassName;
	DEBUG_WRITE("ExtendsCallMethodHandler: extend full name %s", fullClassName.c_str());

	auto isolate = info.GetIsolate();
	auto cachedData = GetCachedExtendedClassData(isolate, fullExtendedName);
	if (cachedData.extendedCtorFunction != nullptr)
	{
		auto cachedExtendedCtorFunc = Local<Function>::New(isolate, *cachedData.extendedCtorFunction);
		info.GetReturnValue().Set(cachedExtendedCtorFunc);
		return;
	}

	auto implementationObjectPropertyName = V8StringConstants::GetClassImplementationObject();
	//reuse validation - checks that implementationObject is not reused for different classes
	auto implementationObjectProperty = implementationObject->GetHiddenValue(implementationObjectPropertyName).As<String>();
	if (implementationObjectProperty.IsEmpty())
	{
		//mark the implementationObject as such and set a pointer to it's class node inside it for reuse validation later
		implementationObject->SetHiddenValue(implementationObjectPropertyName, String::NewFromUtf8(isolate, fullExtendedName.c_str()));

		//append resolved class to implementation object
		implementationObject->SetHiddenValue(ConvertToV8String(fullExtendedName), External::New(Isolate::GetCurrent(), generatedClass));
	}
	else
	{
		string usedClassName = ConvertToString(implementationObjectProperty);
		stringstream s;
		s << "This object is used to extend another class '" << usedClassName << "'";
		ExceptionUtil::GetInstance()->ThrowExceptionToJs(s.str());
		return;
	}

	auto baseClassCtorFunc = node->GetConstructorFunction(isolate);
	auto extendData = External::New(isolate, new ExtendedClassData(node, extendNameAndLocation, implementationObject, fullExtendedName));
	auto extendFuncTemplate = FunctionTemplate::New(isolate, ExtendedClassConstructorCallback, extendData);
	auto extendFunc = extendFuncTemplate->GetFunction();
	auto prototypeName = ConvertToV8String("prototype");
	implementationObject->SetPrototype(baseClassCtorFunc->Get(prototypeName));
	implementationObject->SetAccessor(ConvertToV8String("super"), SuperAccessorGetterCallback, nullptr, implementationObject);
	extendFunc->Set(prototypeName, implementationObject);
	extendFunc->SetPrototype(baseClassCtorFunc);
	SetClassAccessor(extendFunc);
	SetTypeMetadata(isolate, extendFunc, new TypeMetadata(fullExtendedName));
	info.GetReturnValue().Set(extendFunc);

	s_name2NodeCache.insert(make_pair(fullExtendedName, node));

	ExtendedClassCacheData cacheData(extendFunc, fullExtendedName, node);
	s_extendedCtorFuncCache.insert(make_pair(fullExtendedName, cacheData));
}
void MetadataNode::SetInstanceMetadata(Isolate *isolate, Handle<Object> value, MetadataNode *node)
{
	auto key = Local<String>::New(isolate, *s_metadataKey);
	value->SetHiddenValue(key, External::New(isolate, node));
}
void MetadataNode::SetPackageMetadata(Isolate *isolate, Handle<Object> value, MetadataNode *node)
{
	value->SetHiddenValue(ConvertToV8String("tns::PackageMetadata"), External::New(isolate, node));
}
Ejemplo n.º 17
0
   Handle<Object> WrapComponent(Handle<Object> wrappedes, ScriptSystem* scriptsys, dtEntity::EntityId eid, dtEntity::Component* v)
   {

      HandleScope handle_scope;

      Handle<Object> wrapped = scriptsys->GetFromComponentMap(v->GetType(), eid);
      if(!wrapped.IsEmpty())
      {
         return wrapped;
      }

      Handle<FunctionTemplate> templt = GetScriptSystem()->GetTemplateBySID(s_componentWrapper);
      if(templt.IsEmpty())
      {

         templt = FunctionTemplate::New();

         templt->SetClassName(String::New("Component"));
         templt->InstanceTemplate()->SetInternalFieldCount(1);

         Handle<ObjectTemplate> proto = templt->PrototypeTemplate();

         proto->Set("equals", FunctionTemplate::New(COEquals));
         proto->Set("getType", FunctionTemplate::New(COGetType));
         proto->Set("properties", FunctionTemplate::New(COProperties));
         proto->Set("toString", FunctionTemplate::New(COToString));
         proto->Set("finished", FunctionTemplate::New(COFinished));
         proto->Set("copyPropertyValues", FunctionTemplate::New(COCopyPropertyValues));

         GetScriptSystem()->SetTemplateBySID(s_componentWrapper, templt);
      }


      Local<Object> instance = templt->GetFunction()->NewInstance();
      instance->SetInternalField(0, External::New(v));
      instance->SetHiddenValue(scriptsys->GetEntityIdString(), Uint32::New(eid));

      // GetStringFromSID and conversion to v8::String is costly, create a 
      // hidden value in entity system wrapper that stores
      // strings and their string ids as name=>value pairs
      Handle<Value> propnamesval = wrappedes->GetHiddenValue(scriptsys->GetPropertyNamesString());
      if(propnamesval.IsEmpty())
      {
         Handle<Object> names = Object::New();
         dtEntity::PropertyGroup::const_iterator i;
         const dtEntity::PropertyGroup& props = v->Get();
         for(i = props.begin(); i != props.end(); ++i)
         {
            dtEntity::StringId sid = i->first;
            std::string propname = dtEntity::GetStringFromSID(sid);
            names->Set(String::New(propname.c_str()), WrapSID(sid));
         }
         wrappedes->SetHiddenValue(scriptsys->GetPropertyNamesString(), names);
         propnamesval = names;
      }

      Handle<Object> propnames = Handle<Object>::Cast(propnamesval);
      Handle<Array> keys = propnames->GetPropertyNames();
      for(unsigned int i = 0; i < keys->Length(); ++i)
      {
         Handle<String> str = keys->Get(i)->ToString();
         dtEntity::StringId sid = UnwrapSID(propnames->Get(str));
         dtEntity::Property* prop = v->Get(sid);
         if(prop == NULL)
         {
            LOG_ERROR("Could not find property in component: " << ToStdString(str));
            continue;
         }
         Handle<External> ext = v8::External::New(static_cast<void*>(prop));
         instance->SetAccessor(str, COPropertyGetter, COPropertySetter, ext);
      }
      
      // store wrapped component to script system
      scriptsys->AddToComponentMap(v->GetType(), eid, instance);
      return handle_scope.Close(instance);
   }