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
valueOfand 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());
};
本文探讨了如何在JavaScript中封装Python对象,通过实现常见的访问器方法如valueOf和toString,并覆盖对象作为函数调用的行为。此外,还介绍了如何控制属性访问以加载子Python对象。

被折叠的 条评论
为什么被折叠?



