So what we've got is helpful — we've peeked into Python, said "hi", and left just as quickly. For the moment, that'sall we're going to do with python. We need to go back into Javascript-land, and figure out how to make an object thatcan wrap our adorable little PyObject*
's. We'll probably want to provide the typical javascript accessors valueOf
and toString
, not to mention overriding what happens when we call the objects as a function. Property access shouldbe controlled so we can attempt to load up PyObject*
children of the current PyObject*
. Wow! That's a mouthful.
// assuming that we have python_function_template_
// static Persistent<FunctionTemplate> python_function_template_;
static void
Initialize(Handle<Object> target) {
HandleScope scope;
Local<FunctionTemplate> fn_tpl = FunctionTemplate::New();
Local<ObjectTemplate> obj_tpl = fn_tpl->InstanceTemplate();
obj_tpl->SetInternalFieldCount(1);
// this has first priority. see if the properties already exist on the python object
obj_tpl->SetNamedPropertyHandler(Get, Set);
// If we're calling `toString`, delegate to our version of ToString
obj_tpl->SetAccessor(String::NewSymbol("toString"), ToStringAccessor);
// Python objects can be called as functions.
obj_tpl->SetCallAsFunctionHandler(Call, Handle<Value>());
python_function_template_ = Persistent<FunctionTemplate>::New(fn_tpl);
// let's also export "import"
Local<FunctionTemplate> import = FunctionTemplate::New(Import);
target->Set(String::New("import"), import->GetFunction());
};