function Base () {
// Initialize Base
}
function Derived () {
Base.call(this); // call Base constructor and initialize Base
"parts"
// Initialize Derived
}
Derived.prototype = new Base;
Derived.prototype.constructor = Derived;
How do I allow for this to work if Base is a wrapped native instance
with an internal field, and Derived is a script only object?
When using Signature, args.Holder() seems to contain the prototype
instance of Base, not the inherited script Derived instance.
yours,
anton.
On Tue, Mar 16, 2010 at 7:07 AM, Anton Muhin <ant...@google.com> wrote:
> That's expected behaviour. Roughly: for native methods to operate
> properly they assume holder naturally holds native data. But you
> always have This() at your disposal to either find Derived or just
> read properties in normal JS way.
>
> hth and yours,
> anton.
>> --
>> v8-users mailing list
>> v8-u...@googlegroups.com
>> http://groups.google.com/group/v8-users
>>
>
But a new problem arose. If the user should forget to call the Base
constructor in the Derived constructor, my native Base constructor is
never called for the newly created Derived instance!
Is there a callback for "new", when an instance of the prototype is
created?
If I do:
function Array2 () {}
Array2.prototype = new Array;
Array2.prototype.constructor = Array2;
The Array2 is correctly initialized, without having to
Array.call(this) in the constructor.
--- 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;
}
"Derived2.prototype = new Base; \n"
"Derived2.prototype.constructor = Derived2; \n"
I don't want to force users to do it the V8 way, I want them to do it
the JavaScript way.
You write in your derived constructor:
function MyPanel() {
var argv = Array.prototype.slice.apply(arguments,[0]);
this.prototype = this.__proto__ = new ncurses.NCPanel(argv);
// ...
return this;
}
What is the property this.prototype?
In the constructor, isn't "this" the instance, then it has no
"prototype" property, that is on the constructor (function).
I don't see why I should use the "__proto__" property to change the
prototype chain after the instance was created, the prototype chain
has already been specified.
On Mar 17, 11:38 am, Stephan Beal <sgb...@googlemail.com> wrote:
> On Wed, Mar 17, 2010 at 2:44 AM, Henrik Lindqvist <
>
function Base () {}
function Derived () {
Base.call(this);
}
Derived.prototype = new Base;
Derived.prototype.constructor = Derived;
If so, how do I allow for natives to be inherited in v8?
Is this a v8 or JavaScript limitation, or it just can't be done?
> 2010/3/17 Henrik Lindqvist <henrik.lindqv...@gmail.com>
If so, how do I allow for natives to be inherited in v8?
Is this a v8 or JavaScript limitation, or it just can't be done?
On Mar 17, 6:15 pm, Stephan Beal <sgb...@googlemail.com> wrote:
> On Wed, Mar 17, 2010 at 5:12 PM, Henrik Lindqvist <
>
In my library, not the test code above, I walk the prototype chain
looking for a native instance with an internal field. Your v8-juice
seems to do the same.
How would the native code know if walking the prototype chain is
needed or not? Whouldn't it depend on how the user calls functions in
JavaScript code?
How would the native code know if walking the prototype chain isneeded or not? Whouldn't it depend on how the user calls functions in
JavaScript code?
I now understand that users have to use __proto__. Then I can rely on
Signature's and Holder.
> <henrik.lindqv...@gmail.com>wrote:
I have a problem with two nested context, one is a master context, and the
other one is a child context.
Some like this:
//This is how use the master context
void UserContext::execute()
{
PageContext*pc=new PageContext();
{
HandleScope hscope;
Context::Scope context_scope(_context);
_context->Global()->Set(String::New("hello"),String::New("hello
world!!"));
_context->Enter();
pc->run();
_context->Exit();
}
delete pc;
}
pc->run() use other context, both share same SecurityToken (sure!!, an
string with an UUID) and child context have got an object ($) with is a
reference of master global object.
Well, a code executed into the child context doesn't have access to $ member
'hello', in fact $ is undefined or empty (depends of the code). Never I
couldn't see 'hello' member into $ (or master global object).
Is it normal? (sure not, but I don't know the solution)