On Oct 6, 2008, at 6:57 PM, Jacob wrote:
> What problems did you encounter exactly? I plan to update my original
> example code to reflect all the information gathered in this
> discussion, so people can have a solid example to work from.
I am wrapping a C++ class into a JavaScript class "Buffer". As far as
I may see, I proceed along the lines of process.cc example. I create
an object template for "Buffer"
Handle<ObjectTemplate> MakeBufferTemplate () {
HandleScope handle_scope;
Handle<ObjectTemplate> result = ObjectTemplate::New();
result->SetInternalFieldCount(1);
result->Set(String::New("append"),
FunctionTemplate::New(BufferAppend));
then I put the template into a global variable evb_templ
void InstallBuffer (Local<ObjectTemplate> global) {
HandleScope scope;
Handle<ObjectTemplate> raw_template = MakeBufferTemplate();
evb_templ = Persistent<ObjectTemplate>::New(raw_template);
Handle<FunctionTemplate> create_templ =
FunctionTemplate::New(BufferCreate);
create_templ->SetClassName(String::New("Buffer"));
global->Set(String::New("Buffer"), create_templ );
The BufferCreate functions does
Handle<Object> result = evb_templ->NewInstance();
Handle<External> buf_ptr = External::New(buf);
result->SetInternalField(0, buf_ptr);
Basically, it works. Except, when I try to extend the class in my
JavaScript code, it fails:
Buffer.prototype.pollObject = BufferPollBencoded;
var buf = new Buffer();
for (key in buf)
print("def: "+key);
Further, in the output I see e.g.
def: append
... but no "def: pollObject"; later it says that
js/base.js:70: TypeError: Object [object Object] has no method
'pollObject'
var torrent = buf.pollObject(specials);
So, Buffer.prototype does not seem to affect the result of new
Buffer(). Where is the mistake?
--
V
--~--~---------~--~----~------------~-------~--~----~
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
-~----------~----~----~----~------~----~------~--~---