Hello, At some point in the past I've made this small test with V8 to compare interceptor and accessors. During the workshop me and Jedrzej (I think) added more variantions to compare. Kent asked so here it is. :-)
Cheers, -- Caio Marcelo de Oliveira Filho OpenBossa - INdT
#include <v8.h>
using namespace v8;
// Utilities borrowed from shell.cc
// Reads a file into a v8 string.
v8::Handle<v8::String> ReadFile(const char* name) {
FILE* file = fopen(name, "rb");
if (file == NULL) return v8::Handle<v8::String>();
fseek(file, 0, SEEK_END);
int size = ftell(file);
rewind(file);
char* chars = new char[size + 1];
chars[size] = '\0';
for (int i = 0; i < size;) {
int read = fread(&chars[i], 1, size - i, file);
i += read;
}
fclose(file);
v8::Handle<v8::String> result = v8::String::New(chars, size);
delete[] chars;
return result;
}
// Extracts a C string from a V8 Utf8Value.
const char* ToCString(const v8::String::Utf8Value& value) {
return *value ? *value : "<string conversion failed>";
}
// The callback that is invoked by v8 whenever the JavaScript 'print'
// function is called. Prints its arguments on stdout separated by
// spaces and ending with a newline.
v8::Handle<v8::Value> Print(const v8::Arguments& args) {
bool first = true;
for (int i = 0; i < args.Length(); i++) {
v8::HandleScope handle_scope;
if (first) {
first = false;
} else {
printf(" ");
}
v8::String::Utf8Value str(args[i]);
const char* cstr = ToCString(str);
printf("%s", cstr);
}
printf("\n");
fflush(stdout);
return v8::Undefined();
}
static Handle<Value> getter(Local<String>, const AccessorInfo&)
{
return Integer::New(42);
}
Persistent<Integer> theInteger;
static Handle<Value> getterInterceptor(Local<String> property, const AccessorInfo&)
{
// Just passing by...
return Integer::New(42);//Handle<Value>(theInteger);
//return Handle<Value>();
}
static Handle<Value> setterInterceptor(Local<String>, Local<Value>, const AccessorInfo&)
{
return Handle<Value>();
}
int main()
{
HandleScope h;
Persistent<Context> context = Context::New();
Context::Scope contextScope(context);
theInteger = Persistent<Integer>::New(Integer::New(42));
Handle<FunctionTemplate> templateAccessor = FunctionTemplate::New();
templateAccessor->InstanceTemplate()->SetAccessor(String::New("x"), getter);
Handle<FunctionTemplate> templateInterceptor = FunctionTemplate::New();
templateInterceptor->InstanceTemplate()->SetNamedPropertyHandler(getterInterceptor);
Handle<FunctionTemplate> templateBoth = FunctionTemplate::New();
templateBoth->InstanceTemplate()->SetAccessor(String::New("x"), getter);
templateBoth->InstanceTemplate()->SetNamedPropertyHandler(getterInterceptor);
Handle<FunctionTemplate> justSetterInterceptor = FunctionTemplate::New();
justSetterInterceptor->InstanceTemplate()->SetNamedPropertyHandler(0, setterInterceptor);
context->Global()->Set(String::New("print"), FunctionTemplate::New(Print)->GetFunction());
context->Global()->Set(String::New("Accessor"), templateAccessor->GetFunction());
context->Global()->Set(String::New("Interceptor"), templateInterceptor->GetFunction());
context->Global()->Set(String::New("Both"), templateBoth->GetFunction());
context->Global()->Set(String::New("JustSetter"), justSetterInterceptor->GetFunction());
Handle<String> source = ReadFile("test.js");
Handle<Script> script = Script::Compile(source);
script->Run();
}
test.js
Description: JavaScript source
_______________________________________________ Qt-script mailing list [email protected] http://lists.qt.nokia.com/mailman/listinfo/qt-script
