/***********************************************/ /** Embedder's Guide */ /** http://code.google.com/apis/v8/embed.html **/ /***********************************************/ /** * Constant **/ v8::True(); v8::False(); v8::Null(); v8::Undefined(); /** * Some classes **/ v8::Boolean v8::Array v8::Arguments v8::Date v8::Function v8::Int32 v8::Integer v8::Number v8::Object v8::Regexp v8::Primitive v8::Script v8::String v8::Uint32 v8::TryCatch v8::V8 /** * Function signature **/ static v8::Handle<v8::Value> FUNCTION(const v8::Arguments& args); /** * Set simple property **/ process->Set(String::NewSymbol("platform"), String::New(PLATFORM)); /** * Return object **/ Handle<Object> NewObj(int x) { HandleScope handle_scope; Handle<Object> obj = Object::New(); obj->Set(String::NewSymbol("name"), Integer::New(x)); return handle_scope.Close(obj);} /** * Return array **/ Handle<Array> NewPointArray(int x, int y, int z) { // We will be creating temporary handles so we use a handle scope. HandleScope handle_scope; // Create a new empty array. Handle<Array> array = Array::New(3); //if can not determine the length of the array just left first parameter empty Handle<Array> array = Array::New(); // Return an empty result if there was an error creating the array. if (array.IsEmpty()) return Handle<Array>(); // Fill out the values array->Set(0, Integer::New(x)); array->Set(1, Integer::New(y)); array->Set(2, Integer::New(z)); // Return the value through Close. return array; //ERROR // The Close method copies the value of its argument into the enclosing scope // return handle_scope.Close(array); } /** * Accessor * accessor callbacks are invoked when a specific object property is accessed by a script **/ Handle<ObjectTemplate> global_templ = ObjectTemplate::New(); global_templ->SetAccessor(String::New("x"), XGetter, XSetter); global_templ->SetAccessor(String::New("y"), YGetter, YSetter); Handle<Value> XGetter(Local<String> property, const AccessorInfo& info) { return Integer::New(x); } void XSetter(Local<String> property, Local<Value> value, const AccessorInfo& info) { x = value->Int32Value(); } /** * Interceptor * interceptor callbacks are invoked when any object property is accessed by a script * named property interceptors - called when accessing properties with string names. * ndexed property interceptors - called when accessing indexed properties. **/ Local<ObjectTemplate> envTemplate = ObjectTemplate::New(); envTemplate->SetNamedPropertyHandler(EnvGetter, EnvSetter, EnvQuery, EnvDeleter, EnvEnumerator, Undefined()); Local<Object> env = envTemplate->NewInstance();process->Set(String::NewSymbol("env"), env); //Getter static Handle<Value> EnvGetter(Local<String> property, const AccessorInfo& info) { String::Utf8Value key(property); const char* val = getenv(*key); if (val) { HandleScope scope; return scope.Close(String::New(val)); } return Undefined(); } //Setter static Handle<Value> EnvSetter(Local<String> property, Local<Value> value, const AccessorInfo& info) { String::Utf8Value key(property); String::Utf8Value val(value); #ifdef __POSIX__ setenv(*key, *val, 1); #else __WIN32__ NO_IMPL_MSG(setenv) return value; } //Query static Handle<Integer> EnvQuery(Local<String> property, const AccessorInfo& info) { String::Utf8Value key(property); if (getenv(*key)) { HandleScope scope; return scope.Close(Integer::New(None)); } return Handle<Integer>(); } //Deleter static Handle<Boolean> EnvDeleter(Local<String> property, const AccessorInfo& info) { String::Utf8Value key(property); if (getenv(*key)) { #ifdef __POSIX__ unsetenv(*key); #else NO_IMPL_MSG(unsetenv) #endif return True(); } return False(); } //Enumerator static Handle<Array> EnvEnumerator(const AccessorInfo& info) { HandleScope scope; int size = 0; while (environ[size]) size++; Local<Array> env = Array::New(size); for (int i = 0; i < size; ++i) { const char* var = environ[i]; const char* s = strchr(var, '='); const int length = s ? s - var : strlen(var); env->Set(i, String::New(var, length)); } return scope.Close(env); } /** * Create function object and set it's properties **/ /** * Exception **/ TryCatch trycatch; Handle v = script->Run(); if (v.IsEmpty()) { Handle<value> exception = trycatch.Exception(); String::AsciiValue exception_str(exception); printf("Exception: %s\n", *exception_str); // ... } if (args.Length() < 3 || !args[0]->IsString() || !args[1]->IsInt32() || !args[2]->IsInt32()) { return ThrowException( Exception::TypeError(String::New("Bad argument"))); } /** * prototype **/ Handle<FunctionTemplate> biketemplate = FunctionTemplate::New(); biketemplate.PrototypeTemplate().Set( String::New("wheels"), FunctionTemplate::New(MyWheelsMethodCallback)) /** * Inheritance **/ void Inherit(Handle<FunctionTemplate> parent);biketmplate.Inherit(transportTemplate); /** * Parameter conversion **/ //int64 int n = args[0]->IntegerValue(); return scope.Close(Number::New(n)); //int32 int n = args[0]->Int32Value(); //double double d = args[0]->NumberValue(); //uint32 unsigned int ui = args[0]->Uint32Value(); //const char* v8::String:Utf8Value str(args[0]); const char* cstr = *str ? *str : NULL;