Create js object in global object in c++

137 views
Skip to first unread message

Сергей Совершенный

unread,
Mar 10, 2014, 5:11:22 PM3/10/14
to v8-u...@googlegroups.com
How to create js object like "document" in Chrome, with methods and properties
for example: document.getElementById("");
and: document.readyState;

Gregory Hlavac

unread,
Mar 10, 2014, 5:31:58 PM3/10/14
to v8-u...@googlegroups.com
Global object on the context.

Сергей Совершенный

unread,
Mar 10, 2014, 5:43:02 PM3/10/14
to v8-u...@googlegroups.com
Example code?

вторник, 11 марта 2014 г., 1:31:58 UTC+4 пользователь Gregory Hlavac написал:
Global object on the context.

Gregory Hlavac

unread,
Mar 10, 2014, 5:51:03 PM3/10/14
to v8-u...@googlegroups.com
How I do it in my game, mind you I can't show everything as its under a NDA.

tmpl->SetAccessor(String::New("ScriptManager"), ScriptManager::GetGlobal);
(Tmpl is the global template)

And GetGlobal is

void ScriptManager::GetGlobal(Local<String> property, const PropertyCallbackInfo<Value>& info)
{
HandleScope scope(info.GetIsolate());

if(jsGlobal.IsEmpty())
{
Handle<FunctionTemplate> tmpl = FunctionTemplate::New();
tmpl->SetClassName(String::New("ScriptManager"));

Handle<ObjectTemplate> protoTmpl = tmpl->PrototypeTemplate();

protoTmpl->Set(String::New("registerMission"), FunctionTemplate::New(ScriptManager::RegisterMission));
protoTmpl->Set(String::New("toString"), FunctionTemplate::New(ScriptManager::ToString));

Handle<Function> ctorFunction = tmpl->GetFunction();
Handle<Object> result = ctorFunction->NewInstance();

BindEnumerations(result);

jsGlobal.Reset(info.GetIsolate(), result);
}

info.GetReturnValue().Set(jsGlobal);
}

So its pretty much just a hidden class that can never be instantiated more then once, with functions and variables on it.

Gregory Hlavac

unread,
Mar 10, 2014, 5:52:08 PM3/10/14
to v8-u...@googlegroups.com
Also right, jsGlobal is..

Persistent<Object> ScriptManager::jsGlobal;

Сергей Совершенный

unread,
Mar 10, 2014, 7:06:46 PM3/10/14
to v8-u...@googlegroups.com
Thank you ))))))))))))) Very very

вторник, 11 марта 2014 г., 1:52:08 UTC+4 пользователь Gregory Hlavac написал:

Сергей Совершенный

unread,
Mar 10, 2014, 8:17:02 PM3/10/14
to v8-u...@googlegroups.com
How to create objects like class, ex:
var a = new MyObject();
a.callMyFunc(1,2,3);
a.name = "foo";

вторник, 11 марта 2014 г., 1:52:08 UTC+4 пользователь Gregory Hlavac написал:

Gregory Hlavac

unread,
Mar 11, 2014, 11:03:20 AM3/11/14
to v8-u...@googlegroups.com
Vec2Wrapper::Initialize(tmpl, iso);

void Initialize(Handle<ObjectTemplate> exports, Isolate* iso)
{
HandleScope scope(iso);

Local<FunctionTemplate> funcTmpl = FunctionTemplate::New(New);
funcTmpl->SetClassName(String::NewSymbol("vec2"));

Local<ObjectTemplate> protoTmpl = funcTmpl->PrototypeTemplate();
protoTmpl->SetInternalFieldCount(2);
protoTmpl->Set(String::New("add"), FunctionTemplate::New(OperatorAdd));
protoTmpl->Set(String::New("sub"), FunctionTemplate::New(OperatorSubtract));
protoTmpl->Set(String::New("mul"), FunctionTemplate::New(OperatorMultiply));
protoTmpl->Set(String::New("div"), FunctionTemplate::New(OperatorDivide));
protoTmpl->Set(String::New("length"), FunctionTemplate::New(Length));
protoTmpl->Set(String::New("normalize"), FunctionTemplate::New(Normalize));
protoTmpl->Set(String::New("distance"), FunctionTemplate::New(Distance));
protoTmpl->Set(String::New("equals"), FunctionTemplate::New(Equals));
protoTmpl->Set(String::New("toString"), FunctionTemplate::New(ToString));

protoTmpl->SetAccessor(String::NewSymbol("x"), GetVecX, SetVecX);
protoTmpl->SetAccessor(String::NewSymbol("y"), GetVecY, SetVecY);

exports->Set(String::NewSymbol("vec2"), funcTmpl);
if(fncTmpl.IsEmpty())
fncTmpl.Reset(iso, funcTmpl);
}


void New(const FunctionCallbackInfo<Value>& args)
{
if(!args.IsConstructCall())
ThrowException(String::New("Cannot call vec2 constructor as a function."));

HandleScope scope(Isolate::GetCurrent());

float x = args[0]->IsUndefined() ? 0 : args[0]->NumberValue();
float y = args[1]->IsUndefined() ? 0 : args[1]->NumberValue();

vec2* vec = new vec2(x, y);

args.GetReturnValue().Set(Wrap(vec));
}


static Persistent<FunctionTemplate> fncTmpl;
static Persistent<ObjectTemplate> objTmpl;

Gregory Hlavac

unread,
Mar 11, 2014, 11:06:20 AM3/11/14
to v8-u...@googlegroups.com
Another note, this is code from an older version of V8 used by my game, so some of the things these days may need to be passed an isolate as well, so you're gonna want to adjust appropriately.

Сергей Совершенный

unread,
Mar 11, 2014, 11:50:43 AM3/11/14
to v8-u...@googlegroups.com
Thank you!!!!!
How call js function or method from c++ ???
example: i want call application.onStart();
Can i override predefined in c++ function from js ?

вторник, 11 марта 2014 г., 19:06:20 UTC+4 пользователь Gregory Hlavac написал:

Gregory Hlavac

unread,
Mar 11, 2014, 2:29:57 PM3/11/14
to v8-u...@googlegroups.com
Use

Handle<Function>

However I don't have a small or easily accessible way to post this, so you're gonna have to look it up/figure it out, it isn't hard.

Сергей Совершенный

unread,
Mar 12, 2014, 7:01:23 AM3/12/14
to v8-u...@googlegroups.com
Thanks))))

вторник, 11 марта 2014 г., 22:29:57 UTC+4 пользователь Gregory Hlavac написал:

Сергей Совершенный

unread,
Mar 24, 2014, 1:51:48 PM3/24/14
to v8-u...@googlegroups.com
How save function to class public variable, and call later?

среда, 12 марта 2014 г., 15:01:23 UTC+4 пользователь Сергей Совершенный написал:

Gregory Hlavac

unread,
Mar 24, 2014, 6:32:08 PM3/24/14
to v8-u...@googlegroups.com
You don't have to explicitly have that field declared on the C++ side as you can just set new fields on a new instance of anything if you want.

And you don't say if you want to call that function from native side or JS side.

LestaD

unread,
Mar 25, 2014, 7:06:35 AM3/25/14
to v8-u...@googlegroups.com
I make event listener. I must save JS function into vector. And call list from C++.

вторник, 25 марта 2014 г., 2:32:08 UTC+4 пользователь Gregory Hlavac написал:

LestaD

unread,
Mar 31, 2014, 1:01:02 PM3/31/14
to v8-u...@googlegroups.com
Can you help?
please!
Reply all
Reply to author
Forward
0 new messages