__defineGetter__ and friends are not part of the standard and i would
prefer not
to expose them through the API. It is very easy to use these from C++
already so
I don't see a lot of value in exposing them:
#include <assert.h>
#include <stdio.h>
#include <v8.h>
v8::Handle<v8::Value> CompileAndRun(const char* code) {
v8::Handle<v8::Script> script =
v8::Script::Compile(v8::String::New(code));
return script->Run();
}
v8::Handle<v8::Function> GetGlobalFunction(const char* name) {
v8::Handle<v8::Value> function = CompileAndRun(name);
assert(function->IsFunction());
return v8::Handle<v8::Function>::Cast(function);
}
int main() {
v8::HandleScope handle_scope;
v8::Persistent<v8::Context> context = v8::Context::New();
context->Enter();
// Create object.
v8::Handle<v8::Object> object = v8::Object::New();
// Extract __defineGetter__ function.
v8::Handle<v8::Function> define_getter =
GetGlobalFunction("__defineGetter__");
// Define a getter on the object.
v8::Handle<v8::String> name = v8::String::New("x");
v8::Handle<v8::Value> getter = CompileAndRun("(function() { return 42;
})");
v8::Handle<v8::Value> args[] = { name, getter };
define_getter->Call(object, 2, args);
// Get the property and verify the result.
v8::Handle<v8::Value> value = object->Get(name);
assert(value->Int32Value() == 42);
context->Exit();
context.Dispose();
}
http://codereview.chromium.org/7331035/
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev