Beispiel #1
0
void Shape::Initialize(Handle<Object> target)
{
  HandleScope scope;

  Handle<FunctionTemplate> c = FunctionTemplate::New(Shape::New);
  c->InstanceTemplate()->SetInternalFieldCount(1);
  c->SetClassName(String::NewSymbol("shapeObj"));

  SET_ATTRIBUTE_RO(c, "numvalues", getProp);
  SET_ATTRIBUTE_RO(c, "numlines", getProp);
  SET_ATTRIBUTE_RO(c, "index", getProp);
  SET_ATTRIBUTE_RO(c, "type", getProp);
  SET_ATTRIBUTE_RO(c, "tileindex", getProp);
  SET_ATTRIBUTE_RO(c, "classindex", getProp);
  SET_ATTRIBUTE(c, "text", getProp, setProp);

  NODE_SET_PROTOTYPE_METHOD(c, "clone", clone);
  NODE_SET_PROTOTYPE_METHOD(c, "line", getLine);
  NODE_SET_PROTOTYPE_METHOD(c, "add", addLine);
  NODE_SET_PROTOTYPE_METHOD(c, "setGeometry", setGeometry);

  NODE_DEFINE_CONSTANT(c->GetFunction(),
                       "Point",MS_SHAPE_POINT);
  NODE_DEFINE_CONSTANT(c->GetFunction(),
                       "Line",MS_SHAPE_LINE);
  NODE_DEFINE_CONSTANT(c->GetFunction(),
                       "Polygon",MS_SHAPE_POLYGON);
  NODE_DEFINE_CONSTANT(c->GetFunction(),
                       "Null",MS_SHAPE_NULL);

  target->Set(String::NewSymbol("shapeObj"), c->GetFunction());

  constructor.Reset(Isolate::GetCurrent(), c);
}
   v8::Handle<v8::Function> CreateDebugDrawManager(Handle<Context> context)
   {
      v8::HandleScope handle_scope;

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

        templt = FunctionTemplate::New();
        templt->SetClassName(String::New("DebugDrawManager"));
        templt->InstanceTemplate()->SetInternalFieldCount(1);

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

        proto->Set("addAABB", FunctionTemplate::New(DebugDrawManagerAddAABB));
        proto->Set("addCircle", FunctionTemplate::New(DebugDrawManagerAddCircle));
        proto->Set("addCross", FunctionTemplate::New(DebugDrawManagerAddCross));
        proto->Set("addLine", FunctionTemplate::New(DebugDrawManagerAddLine));
        proto->Set("addLines", FunctionTemplate::New(DebugDrawManagerAddLines));
        proto->Set("addSphere", FunctionTemplate::New(DebugDrawManagerAddSphere));
		  proto->Set("addString", FunctionTemplate::New(DebugDrawManagerAddString));
        proto->Set("addTriangle", FunctionTemplate::New(DebugDrawManagerAddTriangle));
        proto->Set("clear", FunctionTemplate::New(DebugDrawManagerClear));
        proto->Set("isEnabled", FunctionTemplate::New(DebugDrawManagerIsEnabled));
        proto->Set("setEnabled", FunctionTemplate::New(DebugDrawManagerSetEnabled));
        proto->Set("toString", FunctionTemplate::New(DebugDrawManagerToString));
        
        GetScriptSystem()->SetTemplateBySID(s_debugDrawWrapper, templt);
      }
      return handle_scope.Close(templt->GetFunction());
   }
Beispiel #3
0
   v8::Handle<v8::Object> WrapScreen(ScriptSystem* ss)
   {
      HandleScope handle_scope;
      Handle<Context> context = ss->GetGlobalContext();
      Context::Scope context_scope(context);

      Handle<FunctionTemplate> templt = FunctionTemplate::New();
      templt->SetClassName(String::New("Screen"));

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

      proto->Set("toString", FunctionTemplate::New(SCRToString));
      proto->Set("getPickRay", FunctionTemplate::New(SCRGetPickRay));
      proto->Set("intersect", FunctionTemplate::New(SCRIntersect));
      proto->Set("pickEntity", FunctionTemplate::New(SCRPickEntity));
      proto->Set("convertWorldToScreenCoords", FunctionTemplate::New(SCRConvertWorldToScreenCoords));
      proto->Set("openWindow", FunctionTemplate::New(SCROpenWindow));
      proto->Set("closeWindow", FunctionTemplate::New(SCRCloseWindow));
      proto->Set("getWindowGeometry", FunctionTemplate::New(SCRGetWindowGeometry));
      proto->Set("setWindowGeometry", FunctionTemplate::New(SCRSetWindowGeometry));

      Local<Object> instance = templt->GetFunction()->NewInstance();

      instance->SetAccessor(String::New("lockCursor"), SCRGetLockCursor, SCRSetLockCursor);
      instance->SetAccessor(String::New("showCursor"), SCRGetShowCursor, SCRSetShowCursor);
      instance->SetAccessor(String::New("width"), SCRGetWidth);
      instance->SetAccessor(String::New("height"), SCRGetHeight);
      instance->SetAccessor(String::New("fullScreen"), SCRGetFullScreen, SCRSetFullScreen);

      return handle_scope.Close(instance);
   }
Beispiel #4
0
void AjaxModule::doRequire (v8::Handle<v8::Object> target) {
	HandleScope scope;

	Handle<FunctionTemplate> ft = FunctionTemplate::New(ajax);

	target->Set(String::New("exports"), ft->GetFunction());
}
Beispiel #5
0
void
test_BasicCall()
{
  HandleScope handle_scope;
  Handle<ObjectTemplate> templ = ObjectTemplate::New();
  Handle<FunctionTemplate> fnT = v8::FunctionTemplate::New(AddOne);
  templ->Set("AddOne", fnT);

  Persistent<Context> context = Context::New(NULL, templ);
  Context::Scope context_scope(context);
  Local<Value> addone = context->Global()->Get(String::New("AddOne"));
  do_check_true(!addone.IsEmpty());
  do_check_true(!addone->IsUndefined());
  do_check_true(addone->IsObject());
  do_check_true(addone->IsFunction());
  Local<Function> fn = addone.As<Function>();
  do_check_eq(fn, fnT->GetFunction());
  Local<Number> n = Number::New(0.5);
  Handle<Value> args[] = { n };
  Local<Value> v = fn->Call(context->Global(), 1, args);
  do_check_true(!v.IsEmpty());
  do_check_true(v->IsNumber());
  Local<Number> n2 = v->ToNumber();
  do_check_eq(n2->Value(), 1.5);
  context.Dispose();
}
Beispiel #6
0
bool bind_Signals_Signal(Handle<Object> parent)
{
    ScriptingManager* pManager = ScriptingManager::getSingletonPtr();

    Handle<FunctionTemplate> signal = pManager->getClassTemplate("Athena.Signals.Signal");

    if (signal.IsEmpty())
    {
        // Declaration of the class
        signal = FunctionTemplate::New(Signal_New);
        signal->InstanceTemplate()->SetInternalFieldCount(1);

        // Attributes
        AddAttribute(signal, "disconnected", Signal_IsDisconnected, 0);

        // Methods
        AddMethod(signal, "connect",         Signal_Connect);
        AddMethod(signal, "disconnect",      Signal_Disconnect);
        AddMethod(signal, "fire",            Signal_Fire);

        pManager->declareClassTemplate("Athena.Signals.Signal", signal);
    }

    // Add the class to the parent
    return parent->Set(String::New("Signal"), signal->GetFunction());
}
Beispiel #7
0
bool bind_Base(Handle<Object> parent)
{
    ScriptingManager* pManager = ScriptingManager::getSingletonPtr();

    Handle<FunctionTemplate> base = pManager->getClassTemplate("Tests.Base");

    if (base.IsEmpty())
    {
        // Declaration of the class
        base = FunctionTemplate::New(Base_New);
        base->InstanceTemplate()->SetInternalFieldCount(1);

        // Attributes
        AddAttribute(base, "a", Base_GetA, Base_SetA);

        // Methods
        AddMethod(base, "f", Base_F);
        AddMethod(base, "g", Base_G);

        // Register the class with the Scripting Manager
        pManager->declareClassTemplate("Tests.Base", base);
    }

    // Add the class to the parent
    return parent->Set(String::New("Base"), base->GetFunction());
}
void Projection::Initialize(Handle<Object> target)
{
    HandleScope scope;
    if (projection_template_.IsEmpty())
	    projection_template_ = Persistent<FunctionTemplate>::New(MakeProjectionTemplate());
    Handle<FunctionTemplate> t = projection_template_;
    target->Set(String::NewSymbol("Projection"), t->GetFunction());
}
Beispiel #9
0
bool bind_Transforms(Handle<Object> parent)
{
    ScriptingManager* pManager = ScriptingManager::getSingletonPtr();

    Handle<FunctionTemplate> transforms = pManager->getClassTemplate("Athena.Entities.Transforms");

    if (transforms.IsEmpty())
    {
        assert(!pManager->getClassTemplate("Athena.Entities.Component").IsEmpty());

        // Declaration of the class
        transforms = FunctionTemplate::New(Transforms_New);
        transforms->InstanceTemplate()->SetInternalFieldCount(1);
        transforms->Inherit(pManager->getClassTemplate("Athena.Entities.Component"));

        // Attributes
        AddAttribute(transforms, "position",           Transforms_GetPosition,           Transforms_SetPosition);
        AddAttribute(transforms, "worldPosition",      Transforms_GetWorldPosition,      0);
        AddAttribute(transforms, "orientation",        Transforms_GetOrientation,        Transforms_SetOrientation);
        AddAttribute(transforms, "worldOrientation",   Transforms_GetWorldOrientation,   0);
        AddAttribute(transforms, "inheritOrientation", Transforms_GetInheritOrientation, Transforms_SetInheritOrientation);
        AddAttribute(transforms, "scale",              Transforms_GetScale,              Transforms_SetScale);
        AddAttribute(transforms, "worldScale",         Transforms_GetWorldScale,         0);
        AddAttribute(transforms, "inheritScale",       Transforms_GetInheritScale,       Transforms_SetInheritScale);

        // Methods
        AddMethod(transforms, "translate",        Transforms_Translate);
        AddMethod(transforms, "setDirection",     Transforms_SetDirection);
        AddMethod(transforms, "lookAt",           Transforms_LookAt);
        AddMethod(transforms, "roll",             Transforms_Roll);
        AddMethod(transforms, "pitch",            Transforms_Pitch);
        AddMethod(transforms, "yaw",              Transforms_Yaw);
        AddMethod(transforms, "rotate",           Transforms_Rotate);
        AddMethod(transforms, "resetOrientation", Transforms_ResetOrientation);
        AddMethod(transforms, "rescale",          Transforms_Rescale);

        pManager->declareClassTemplate("Athena.Entities.Transforms", transforms);

        parent->Set(String::New("Transforms_TYPE"), String::New(Athena::Entities::Transforms::TYPE.c_str()));

        parent->Set(String::New("TS_LOCAL"),  Uint32::New(Transforms::TS_LOCAL));
        parent->Set(String::New("TS_PARENT"), Uint32::New(Transforms::TS_PARENT));
        parent->Set(String::New("TS_WORLD"),  Uint32::New(Transforms::TS_WORLD));
    }

    // Add the class to the parent
    return parent->Set(String::New("Transforms"), transforms->GetFunction());
}
Beispiel #10
0
void Geometry::Initialize(Handle<Object> target)
{
    HandleScope scope;
    if (geometry_template_.IsEmpty())
        geometry_template_ = Persistent<FunctionTemplate>::New(MakeGeometryTemplate());
    Handle<FunctionTemplate> t = geometry_template_;
    NODE_SET_PROTOTYPE_METHOD(t, "fromWkt", FromWKT);
    NODE_SET_PROTOTYPE_METHOD(t, "toWkt", ToWKT);
    // Topology operations
    NODE_SET_PROTOTYPE_METHOD(t, "intersection", Intersection);
    NODE_SET_PROTOTYPE_METHOD(t, "buffer", Buffer);
    NODE_SET_PROTOTYPE_METHOD(t, "difference", Difference);
    NODE_SET_PROTOTYPE_METHOD(t, "symDifference", SymDifference);
    NODE_SET_PROTOTYPE_METHOD(t, "union", Union);
    NODE_SET_PROTOTYPE_METHOD(t, "relate", Relate);
    NODE_SET_PROTOTYPE_METHOD(t, "simplify", Simplify);
    // Unary predicates
    NODE_SET_PROTOTYPE_METHOD(t, "isEmpty", IsEmpty);
    NODE_SET_PROTOTYPE_METHOD(t, "isValid", IsValid);
    NODE_SET_PROTOTYPE_METHOD(t, "isSimple", IsSimple);
    NODE_SET_PROTOTYPE_METHOD(t, "isRing", IsRing);
    NODE_SET_PROTOTYPE_METHOD(t, "hasZ", HasZ);
    // Binary predicates
    NODE_SET_PROTOTYPE_METHOD(t, "disjoint", Disjoint);
    NODE_SET_PROTOTYPE_METHOD(t, "touches", Touches);
    NODE_SET_PROTOTYPE_METHOD(t, "preparedIntersects", PreparedIntersects);
    NODE_SET_PROTOTYPE_METHOD(t, "crosses", Crosses);
    NODE_SET_PROTOTYPE_METHOD(t, "within", Within);
    NODE_SET_PROTOTYPE_METHOD(t, "preparedContains", PreparedContains);
    NODE_SET_PROTOTYPE_METHOD(t, "preparedContainsProperly", PreparedContainsProperly);
    NODE_SET_PROTOTYPE_METHOD(t, "preparedCovers", PreparedCovers);
    NODE_SET_PROTOTYPE_METHOD(t, "overlaps", Overlaps);
    NODE_SET_PROTOTYPE_METHOD(t, "contains", Contains);
    NODE_SET_PROTOTYPE_METHOD(t, "equals", Equals);
    NODE_SET_PROTOTYPE_METHOD(t, "equalsExact", EqualsExact);

    NODE_SET_PROTOTYPE_METHOD(t, "distance", Distance);

    target->Set(String::NewSymbol("Geometry"), t->GetFunction());
}
Beispiel #11
0
bool bind_Scene(Handle<Object> parent)
{
    ScriptingManager* pManager = ScriptingManager::getSingletonPtr();

    Handle<FunctionTemplate> scene = pManager->getClassTemplate("Athena.Entities.Scene");

    if (scene.IsEmpty())
    {
        // Declaration of the class
        scene = FunctionTemplate::New(Scene_New);
        scene->InstanceTemplate()->SetInternalFieldCount(1);

        // Attributes
        AddAttribute(scene, "name",         Scene_GetName, 0);
        AddAttribute(scene, "enabled",      Scene_GetEnabled, Scene_SetEnabled);
        AddAttribute(scene, "shown",        Scene_GetShown, Scene_SetShown);
        AddAttribute(scene, "nbEntities",   Scene_GetNbEntities, 0);
        AddAttribute(scene, "components",   Scene_GetComponentsList, 0);
        AddAttribute(scene, "nbComponents", Scene_GetNbComponents, 0);
        AddAttribute(scene, "signals",      Scene_GetSignalsList, 0);

        // Methods - Entities management
        AddMethod(scene, "create",           Scene_CreateEntity);
        AddMethod(scene, "getEntity",        Scene_GetEntity);
        AddMethod(scene, "destroy",          Scene_DestroyEntity);
        AddMethod(scene, "destroyAll",       Scene_DestroyAll);
        AddMethod(scene, "transfer",         Scene_Transfer);

        // Methods - Components management
        AddMethod(scene, "getComponent",     Scene_GetComponent);
        AddMethod(scene, "getMainComponent", Scene_GetMainComponent);

        pManager->declareClassTemplate("Athena.Entities.Scene", scene);
    }

    // Add the class to the parent
    return parent->Set(String::New("Scene"), scene->GetFunction());
}
Beispiel #12
0
   v8::Handle<v8::Object> WrapLogger(Handle<Context> context)
   {
      v8::HandleScope handle_scope;
      Handle<FunctionTemplate> templt = GetScriptSystem()->GetTemplateBySID(s_loggerWrapper);
      if(templt.IsEmpty())
      {
        templt = FunctionTemplate::New();
        templt->SetClassName(String::New("Log"));

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

        proto->Set("always", FunctionTemplate::New(LogAlways));
        proto->Set("debug", FunctionTemplate::New(LogDebug));
        proto->Set("error", FunctionTemplate::New(LogError));
        proto->Set("info", FunctionTemplate::New(LogInfo));
        proto->Set("warning", FunctionTemplate::New(LogWarning));
        proto->Set("addLogListener", FunctionTemplate::New(LogAddListener));
        proto->Set("processLogListeners", FunctionTemplate::New(LogProcessListeners));
        GetScriptSystem()->SetTemplateBySID(s_loggerWrapper, templt);
      }
      Local<Object> instance = templt->GetFunction()->NewInstance();
      return handle_scope.Close(instance);
   }
   v8::Handle<v8::Object> WrapElementDocument(v8::Handle<v8::Context> context, Rocket::Core::ElementDocument* v)
   {
      
      v8::HandleScope handle_scope;

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

        templt = FunctionTemplate::New();
        templt->Inherit(GetElementTemplate());
        templt->SetClassName(String::New("ElementDocument"));
        templt->InstanceTemplate()->SetInternalFieldCount(1);
		  templt->InstanceTemplate()->SetNamedPropertyHandler(
           ELNamedPropertyGetter,
           ELNamedPropertySetter,
           ELNamedPropertyQuery,
           ELNamedPropertyDeleter,
           ELNamedPropertyEnumerator
        );

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

		  proto->Set("hide", FunctionTemplate::New(EDHide));
        proto->Set("toString", FunctionTemplate::New(EDToString));
        proto->Set("show", FunctionTemplate::New(EDShow));
        proto->Set("hide", FunctionTemplate::New(EDHide));
        proto->Set("close", FunctionTemplate::New(EDClose));

        GetScriptSystem()->SetTemplateBySID(s_elementDocumentWrapper, templt);

      }
      Local<Object> instance = templt->GetFunction()->NewInstance();
      instance->SetInternalField(0, External::New(v));
      return handle_scope.Close(instance);

   }
Handle<Function> MetadataNode::SetMembersFromStaticMetadata(Isolate *isolate, Handle<FunctionTemplate>& ctorFuncTemplate, Handle<ObjectTemplate>& prototypeTemplate, MetadataTreeNode *treeNode)
{
	SET_PROFILER_FRAME();

	Handle<Function> ctorFunction;

	uint8_t *curPtr = s_metadataReader.GetValueData() + treeNode->offsetValue + 1;

	auto nodeType = s_metadataReader.GetNodeType(treeNode);

	auto curType = s_metadataReader.ReadTypeName(treeNode);

	curPtr += sizeof(uint16_t /* baseClassId */);

	if (s_metadataReader.IsNodeTypeInterface(nodeType))
	{
		curPtr += sizeof(uint8_t) + sizeof(uint32_t);
	}

	//get candidates from instance methods metadata
	auto instanceMethodCout = *reinterpret_cast<uint16_t*>(curPtr);
	curPtr += sizeof(uint16_t);
	string lastMethodName;
	MethodCallbackData *callbackData = nullptr;
	for (auto i = 0; i < instanceMethodCout; i++)
	{
		auto entry = s_metadataReader.ReadInstanceMethodEntry(&curPtr);
		if (entry.name != lastMethodName)
		{
			callbackData = new MethodCallbackData(this);
			auto funcData = External::New(isolate, callbackData);
			auto funcTemplate = FunctionTemplate::New(isolate, MethodCallback, funcData);
			auto funcName = ConvertToV8String(entry.name);
			prototypeTemplate->Set(funcName, funcTemplate->GetFunction());
			lastMethodName = entry.name;
		}
		callbackData->candidates.push_back(entry);
	}

	//get candidates from instance fields metadata
	auto instanceFieldCout = *reinterpret_cast<uint16_t*>(curPtr);
	curPtr += sizeof(uint16_t);
	for (auto i = 0; i < instanceFieldCout; i++)
	{
		auto entry = s_metadataReader.ReadInstanceFieldEntry(&curPtr);

		auto fieldName = ConvertToV8String(entry.name);
		auto fieldInfo = new FieldCallbackData(entry);
		fieldInfo->declaringType = curType;
		auto fieldData = External::New(isolate, fieldInfo);
		prototypeTemplate->SetAccessor(fieldName, FieldAccessorGetterCallback, FieldAccessorSetterCallback, fieldData, AccessControl::DEFAULT, PropertyAttribute::DontDelete);
	}

	ctorFunction = ctorFuncTemplate->GetFunction();

	//get candidates from static methods metadata
	callbackData = nullptr;
	lastMethodName.clear();
	auto staticMethodCout = *reinterpret_cast<uint16_t*>(curPtr);
	curPtr += sizeof(uint16_t);
	for (auto i = 0; i < staticMethodCout; i++)
	{
		auto entry = s_metadataReader.ReadStaticMethodEntry(&curPtr);
		if (entry.name != lastMethodName)
		{
			callbackData = new MethodCallbackData(this);
			auto funcData = External::New(isolate, callbackData);
			auto funcTemplate = FunctionTemplate::New(isolate, MethodCallback, funcData);
			auto funcName = ConvertToV8String(entry.name);
			ctorFunction->Set(funcName, funcTemplate->GetFunction());
			lastMethodName = entry.name;
		}
		callbackData->candidates.push_back(entry);
	}

	auto extendFuncName = V8StringConstants::GetExtend();
	auto extendFuncTemplate = FunctionTemplate::New(isolate, ExtendCallMethodHandler, External::New(isolate, this));
	ctorFunction->Set(extendFuncName, extendFuncTemplate->GetFunction());

	//get candidates from static fields metadata
	auto staticFieldCout = *reinterpret_cast<uint16_t*>(curPtr);
	curPtr += sizeof(uint16_t);
	for (auto i = 0; i < staticFieldCout; i++)
	{
		auto entry = s_metadataReader.ReadStaticFieldEntry(&curPtr);

		auto fieldName = ConvertToV8String(entry.name);
		auto fieldData = External::New(isolate, new FieldCallbackData(entry));
		ctorFunction->SetAccessor(fieldName, FieldAccessorGetterCallback, FieldAccessorSetterCallback, fieldData, AccessControl::DEFAULT, PropertyAttribute::DontDelete);
	}

	SetClassAccessor(ctorFunction);

	return ctorFunction;
}
Beispiel #15
0
Handle<Value> wrap_lisp_symbol(Lisp_Object sym) {
  Local<Object> obj = symbol_templ->GetFunction()->NewInstance();
  //obj->SetInternalField(0, External::New((void*)sym));
  obj->SetAlignedPointerInInternalField(0, (void*) sym);
  return obj;
}
void CanvasContextV8Bindings::loadScript(const std::string& _filename, OgreCanvas::CanvasContext* _canvasContext, OgreCanvas::CanvasLogger* _console)
{
	CanvasContextV8Bindings::context2D = _canvasContext;
	
	HandleScope handle_scope;
	
	//Console :
		
		//template
		Handle<FunctionTemplate> consoleTemplate = FunctionTemplate::New();
		consoleTemplate->SetClassName(v8::String::New("Console"));
		CanvasContextV8Bindings::consoleTemplate = Persistent<FunctionTemplate>::New(consoleTemplate);

		//prototype
		Handle<ObjectTemplate> consolePrototype = consoleTemplate->PrototypeTemplate();

		//attaching method
		consolePrototype->Set("log", FunctionTemplate::New(log));

		//creating instance
		Handle<ObjectTemplate> consoleInstance = consoleTemplate->InstanceTemplate();
		consoleInstance->SetInternalFieldCount(1);

	//Image :
		
		//template
		Handle<FunctionTemplate> imageTemplate = FunctionTemplate::New();
		imageTemplate->SetClassName(v8::String::New("Image"));
		CanvasContextV8Bindings::imageTemplate = Persistent<FunctionTemplate>::New(imageTemplate);

		//prototype
		Handle<ObjectTemplate> imagePrototype = imageTemplate->PrototypeTemplate();

		//creating instance
		Handle<ObjectTemplate> imageInstance = imageTemplate->InstanceTemplate();
		imageInstance->SetInternalFieldCount(1);

	//Canvas gradient :
		
		//template
		Handle<FunctionTemplate> canvasGradientTemplate = FunctionTemplate::New();
		canvasGradientTemplate->SetClassName(v8::String::New("CanvasGradient"));
		CanvasContextV8Bindings::canvasGradientTemplate = Persistent<FunctionTemplate>::New(canvasGradientTemplate);

		//prototype
		Handle<ObjectTemplate> canvasGradientPrototype = canvasGradientTemplate->PrototypeTemplate();

		//creating instance
		Handle<ObjectTemplate> canvasGradientInstance = canvasGradientTemplate->InstanceTemplate();
		canvasGradientInstance->SetInternalFieldCount(1);

		//attaching method
		canvasGradientPrototype->Set("addColorStop", FunctionTemplate::New(addColorStop));

	//Canvas Pattern :

		//template
		Handle<FunctionTemplate> canvasPatternTemplate = FunctionTemplate::New();
		canvasPatternTemplate->SetClassName(v8::String::New("CanvasPattern"));
		CanvasContextV8Bindings::canvasPatternTemplate = Persistent<FunctionTemplate>::New(canvasPatternTemplate);

		//prototype
		Handle<ObjectTemplate> canvasPatternPrototype = canvasPatternTemplate->PrototypeTemplate();
		
		//creating instance
		Handle<ObjectTemplate> canvasPatternInstance = canvasPatternTemplate->InstanceTemplate();
		canvasPatternInstance->SetInternalFieldCount(1);

	//Canvas context :

		//template
		Handle<FunctionTemplate> canvasContextTemplate = FunctionTemplate::New();
		canvasContextTemplate->SetClassName(v8::String::New("CanvasContext"));
	
		//prototype
		Handle<ObjectTemplate> canvasContextPrototype = canvasContextTemplate->PrototypeTemplate();

		//attaching method

			//2D Context
			canvasContextPrototype->Set("save",        FunctionTemplate::New(save));
			canvasContextPrototype->Set("restore",     FunctionTemplate::New(restore));

			//Transformation
			canvasContextPrototype->Set("scale",        FunctionTemplate::New(scale));
			canvasContextPrototype->Set("rotate",       FunctionTemplate::New(rotate));
			canvasContextPrototype->Set("translate",    FunctionTemplate::New(translate));
			canvasContextPrototype->Set("transform",    FunctionTemplate::New(transform));
			canvasContextPrototype->Set("setTransform", FunctionTemplate::New(setTransform));
			
			//Image drawing
			canvasContextPrototype->Set("drawImage",    FunctionTemplate::New(drawImage));		

			//Colors, styles and shadows
			canvasContextPrototype->Set("createLinearGradient", FunctionTemplate::New(createLinearGradient));
			canvasContextPrototype->Set("createRadialGradient", FunctionTemplate::New(createRadialGradient));
			canvasContextPrototype->Set("createPattern",        FunctionTemplate::New(createPattern));

			//Paths
			canvasContextPrototype->Set("beginPath",        FunctionTemplate::New(beginPath));
			canvasContextPrototype->Set("closePath",        FunctionTemplate::New(closePath));		
			canvasContextPrototype->Set("fill",             FunctionTemplate::New(fill));
			canvasContextPrototype->Set("stroke",           FunctionTemplate::New(stroke));
			canvasContextPrototype->Set("clip",             FunctionTemplate::New(clip));

			canvasContextPrototype->Set("moveTo",           FunctionTemplate::New(moveTo));
			canvasContextPrototype->Set("lineTo",           FunctionTemplate::New(lineTo));
			canvasContextPrototype->Set("quadraticCurveTo", FunctionTemplate::New(quadraticCurveTo));
			canvasContextPrototype->Set("bezierCurveTo",    FunctionTemplate::New(bezierCurveTo));
			canvasContextPrototype->Set("arcTo",            FunctionTemplate::New(arcTo));
			canvasContextPrototype->Set("arc",              FunctionTemplate::New(arc));
			canvasContextPrototype->Set("rect",             FunctionTemplate::New(rect));
			canvasContextPrototype->Set("isPointInPath",    FunctionTemplate::New(isPointInPath));

			//Text
			canvasContextPrototype->Set("fillText",    FunctionTemplate::New(fillText));
			canvasContextPrototype->Set("strokeText",  FunctionTemplate::New(strokeText));
			canvasContextPrototype->Set("measureText", FunctionTemplate::New(measureText));

			//Rectangles
			canvasContextPrototype->Set("clearRect",   FunctionTemplate::New(clearRect));
			canvasContextPrototype->Set("fillRect",    FunctionTemplate::New(fillRect));
			canvasContextPrototype->Set("strokeRect",  FunctionTemplate::New(strokeRect));

			//New
			canvasContextPrototype->Set("saveToPNG",   FunctionTemplate::New(saveToPNG));		
			canvasContextPrototype->Set("clear",       FunctionTemplate::New(clear));

		//creating instance
		Handle<ObjectTemplate> canvasContextInstance = canvasContextTemplate->InstanceTemplate();
		canvasContextInstance->SetInternalFieldCount(1);

		//attaching properties

			//Compositing
			canvasContextInstance->SetAccessor(v8::String::New("globalAlpha"), getterGlobalAlpha, setterGlobalAlpha);
			canvasContextInstance->SetAccessor(v8::String::New("globalCompositeOperation"), getterGlobalCompositeOperation, setterGlobalCompositeOperation);
			
			//Line styles
			canvasContextInstance->SetAccessor(v8::String::New("lineWidth"),   getterLineWidth,  setterLineWidth);
			canvasContextInstance->SetAccessor(v8::String::New("lineCap"),     getterLineCap,    setterLineCap);
			canvasContextInstance->SetAccessor(v8::String::New("lineJoin"),    getterLineJoin,   setterLineJoin);
			canvasContextInstance->SetAccessor(v8::String::New("miterLimit"),  getterMiterLimit, setterMiterLimit);
			canvasContextInstance->SetAccessor(v8::String::New("lineDash"),    getterLineDash,   setterLineDash);
			
			//Colors, styles and shadows
			canvasContextInstance->SetAccessor(v8::String::New("fillStyle"),     getterFillStyle,     setterFillStyle);
			canvasContextInstance->SetAccessor(v8::String::New("strokeStyle"),   getterStrokeStyle,   setterStrokeStyle);
			canvasContextInstance->SetAccessor(v8::String::New("shadowOffsetX"), getterShadowOffsetX, setterShadowOffsetX);
			canvasContextInstance->SetAccessor(v8::String::New("shadowOffsetY"), getterShadowOffsetY, setterShadowOffsetY);
			canvasContextInstance->SetAccessor(v8::String::New("shadowBlur"),    getterShadowBlur,    setterShadowBlur);
			canvasContextInstance->SetAccessor(v8::String::New("shadowColor"),   getterShadowColor,   setterShadowColor);

			//Text
			canvasContextInstance->SetAccessor(v8::String::New("font"),         getterFont,         setterFont);
			canvasContextInstance->SetAccessor(v8::String::New("textAlign"),    getterTextAlign,    setterTextAlign);
			canvasContextInstance->SetAccessor(v8::String::New("textBaseline"), getterTextBaseline, setterTextBaseline);
			
			//New
			canvasContextInstance->SetAccessor(v8::String::New("antialiasing"), getterAntiAliasing, setterAntiAliasing);

	//Image
	Handle<ObjectTemplate> global = ObjectTemplate::New();
	global->Set(String::New("loadImage"), FunctionTemplate::New(loadImage));

	Persistent<Context> context = Context::New(NULL, global);
	CanvasContextV8Bindings::contextV8 = context;

	Context::Scope context_scope(context);

		//building link between js 'ctx' variable and c++ _canvasContext variable
		Handle<Function> canvasContextConstructor = canvasContextTemplate->GetFunction();
		Local<Object> obj = canvasContextConstructor->NewInstance();
		obj->SetInternalField(0, External::New(_canvasContext));
		context->Global()->Set(v8::String::New("ctx"), obj);

		//building link between js 'console' variable and c++ _console variable
		Handle<Function> consoleConstructor = consoleTemplate->GetFunction();
		Local<Object> obj2 = consoleConstructor->NewInstance();
		obj2->SetInternalField(0, External::New(_console));
		context->Global()->Set(v8::String::New("console"), obj2);

	Handle<v8::String> source = v8::String::New(readScript(_filename).c_str());
	Handle<Script> script = Script::Compile(source);
	Handle<Value> result = script->Run();
	/*
	CanvasContextV8Bindings::canvasGradientTemplate.Dispose();
	CanvasContextV8Bindings::canvasPatternTemplate.Dispose();
	CanvasContextV8Bindings::imageTemplate.Dispose();
	*/
}
Beispiel #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);
   }
Beispiel #18
0
	Local<Object> TLNew(uint id,void* cobj){
		Handle<Value> argv[1];
		argv[0] = Uint32::New((UINT_PTR)cobj);
		Handle<FunctionTemplate> ft = GetEnv()->GetTemplate(id);
		return ft->GetFunction()->NewInstance(1,argv);
	}
Handle<Function> MetadataNode::SetMembersFromRuntimeMetadata(Isolate *isolate, Handle<FunctionTemplate>& ctorFuncTemplate, Handle<ObjectTemplate>& prototypeTemplate, MetadataTreeNode *treeNode)
{
	SET_PROFILER_FRAME();

	assert(treeNode->metadata != nullptr);

	string line;
	const string& metadata = *treeNode->metadata;
	stringstream s(metadata);

	string kind;
	string name;
	string signature;
	int paramCount;

	getline(s, line); // type line
	getline(s, line); // base class line

	string lastMethodName;
	MethodCallbackData *callbackData = nullptr;

	while (getline(s, line))
	{
		stringstream tmp(line);
		tmp >> kind >> name >> signature >> paramCount;

		char chKind = kind[0];

		assert((chKind == 'M') || (chKind == 'F'));

		MetadataEntry entry;
		entry.name = name;
		entry.sig = signature;
		entry.paramCount = paramCount;
		entry.isStatic = false;

		if (chKind == 'M')
		{
			if (entry.name != lastMethodName)
			{
				callbackData = new MethodCallbackData(this);
				auto funcData = External::New(isolate, callbackData);
				auto funcTemplate = FunctionTemplate::New(isolate, MethodCallback, funcData);
				auto funcName = ConvertToV8String(entry.name);
				prototypeTemplate->Set(funcName, funcTemplate->GetFunction());
				lastMethodName = entry.name;
			}
			callbackData->candidates.push_back(entry);
		}
		else if (chKind == 'F')
		{
			auto fieldName = ConvertToV8String(entry.name);
			auto fieldData = External::New(isolate, new FieldCallbackData(entry));
			auto access = entry.isFinal ? AccessControl::ALL_CAN_READ : AccessControl::DEFAULT;
			prototypeTemplate->SetAccessor(fieldName, FieldAccessorGetterCallback, FieldAccessorSetterCallback, fieldData, access, PropertyAttribute::DontDelete);
		}
	}

	auto ctorFunction = ctorFuncTemplate->GetFunction();

	return ctorFunction;
}