Anonymous avatar Anonymous committed a6da3e0

hello v8!!!

Comments (0)

Files changed (1)

 
 using namespace v8;
 
+Local<Function> create_func(Persistent<Object>& global,
+                           const char* code, const char* name){
+  Handle<Script> s = Script::Compile(String::New(code), String::New(name));
+  Handle<Value> fun_obj = s->Run();
+  return Function::Cast(*(global->Get(String::New(name))));
+}
+
 int main(int argc, char* argv[]) {
 
   // Create a stack-allocated handle scope.
   HandleScope handle_scope;
 
+  //Handle<ObjectTemplate> global = ObjectTemplate::New();
+
   // Create a new context.
-  Persistent<Context> context = Context::New();
+  Persistent<Context> context = Context::New();//NULL, global);
+  Persistent<Object> global = Persistent<Object>::New(context->Global());
   
   // Enter the created context for compiling and
   // running the hello world script. 
   //  Handle<String> source = String::New("'Hello' + ', World!'");
   // Handle<String> source = String::New("[32, 234, 23/4, {asfd:\"asdf\"}, \"hello\"];");
   //  Handle<String> source = String::New("{ someatom: 23, anotheratom: 3.3 };");
-  const char* pcode =
-    "function map(f, l){"
-    "  var ret = []; for(var i=0, len=l.length; i<len; i++ ) {"
-    "  ret[i] = f(l[i]); } return ret;"
-    "}"
-    "function fold(f, acc0, l){"
-    "  var ret = acc0; for(var i=0, len=l.length; i<len; i++ ) {"
-    "  ret = f(l[i], ret); } return ret;"
-    "};"
-    ;
-  ScriptData* script_data = ScriptData::PreCompile(String::New(pcode));
-  const char* code =
-    //    "function id(__x__){  return __x__; } "
-    "function add(x, y){ return x+y; } "
-    "1+1;";//"add(1, 1);"
-    //    "fold(add, 0, map(id, [10, 23, 23]));"
-    ;
-  Handle<String> source = String::New(code);
-  // Compile the source code.
-  Handle<Script> script = Script::Compile(source, NULL, script_data, Handle<String>());
-  
+
+  Handle<Value> v0[1];
+  v0[0]= Integer::New(23);
+  //  global->Set("v0", 
+  const char* fun = "fun0 = function(i){ return i+1; }";
+  Local<Value> ret = create_func(global, fun, "fun0");
+  Persistent<Value> f = Persistent<Value>::New(ret);
+  Handle<Value> result = ((Function*)(*f))->Call(global, 1, v0);
+
   // Run the script to get the result.
-  Handle<Value> result = script->Run();
+  //  Handle<Value> result = script->Run();
   
   // Dispose the persistent context.
   context.Dispose();
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.