I've made a little test, see source below, that highlight my problem,
here is the output:

--- test 1 ---
Constructor: pointer=0x1   << prototype = new Base
Test: pointer=(nil)
Test: pointer=(nil)
--- test 2 ---
Constructor: pointer=0x2   << prototype = new Base
Constructor: pointer=0x3   << new Derived2
Test: pointer=0x3
Constructor: pointer=0x4   << new Derived2
Test: pointer=0x4
--- test 3 ---
Constructor: pointer=0x5   << prototype = new Base
Constructor: pointer=0x6   << new Derived3
Test: pointer=(nil)
Constructor: pointer=0x7   << new Derived3
Test: pointer=(nil)


Test 1:
If I'd like my native instance (Base) to be inherited as regular built-
in types like Array etc. Where should I
initialize it when the constructor is never called?  Where does Array
initialize it's native parts when inherited?

Test 2:
Work. But only because the Base constructor i called. Built-in's don't
require this.

Test 3:
This is really wierd. Why does adding a property to the Derived
instance make the Constructor create/return, and/or the test function
be called with another "this". Bug?


I'm writing a runtime library for v8. I only want the users to be able
to use all the language features, including correct inheritance.


// g++ -m32 -Iinclude -L. test.cc -lpthread -lv8

#include <v8.h>

using namespace v8;

char* pointer = NULL;

Handle<Value> Constructor (const Arguments& args) {
  args.This()->SetPointerInInternalField(0, ++pointer);
  printf("Constructor: pointer=%p\n", pointer);
  return args.This();
}

Handle<Value> Test (const Arguments& args) {
  printf("Test: pointer=%p\n", args.This()-
>GetPointerFromInternalField(0));
  return Undefined();
}

int main (int argc, char* argv[]) {
  HandleScope scope;
  Handle<ObjectTemplate> global = ObjectTemplate::New();
  Persistent<Context> context = Context::New(NULL, global);
  Context::Scope context_scope(context);

  // native Base
  Handle<String> class_name = String::NewSymbol("Base");
  Handle<FunctionTemplate> constructor =
FunctionTemplate::New(Constructor);
  constructor->SetClassName(class_name);
  Handle<ObjectTemplate> prototype = constructor->PrototypeTemplate();
  prototype->Set("test", FunctionTemplate::New(Test));
  Handle<ObjectTemplate> instance = constructor->InstanceTemplate();
  instance->SetInternalFieldCount(1);
  Context::GetCurrent()->Global()->Set(class_name, constructor-
>GetFunction());

  puts("--- test 1 ---");
  Script::Compile(String::New(
    "function Derived1 () {}                    \n"
    "Derived1.prototype = new Base;             \n"
    "Derived1.prototype.constructor = Derived1; \n"
    "(new Derived1).test();                     \n"
    "(new Derived1).test();                     \n"
  ))->Run();

  puts("--- test 2 ---");
  Script::Compile(String::New(
    "function Derived2 () {                     \n"
    "  Base.call(this);                         \n"
    "}                                          \n"
    "Derived2.prototype = new Base;             \n"
    "Derived2.prototype.constructor = Derived2; \n"
    "(new Derived2).test();                     \n"
    "(new Derived2).test();                     \n"
  ))->Run();

  puts("--- test 3 ---");
  Script::Compile(String::New(
    "function Derived3 () {                     \n"
    "  Base.call(this);                         \n"
    "  this.foo = true;                         \n"
    "}                                          \n"
    "Derived3.prototype = new Base;             \n"
    "Derived3.prototype.constructor = Derived3; \n"
    "(new Derived3).test();                     \n"
    "(new Derived3).test();                     \n"
  ))->Run();

  V8::Dispose();
  return 0;
}

-- 
v8-users mailing list
v8-users@googlegroups.com
http://groups.google.com/group/v8-users

Reply via email to