Commits

UENISHI Kota  committed 658558c

mmmm

  • Participants
  • Parent commits 01a4ca9

Comments (0)

Files changed (1)

File c++/v8/main.cpp

 // from http://code.google.com/apis/v8/get_started.html
 #include <v8.h>
 #include <iostream>
+#include <sstream>
+#include <stdexcept>
 using namespace std;
 using namespace v8;
 
 Local<Function> create_func(Persistent<Object>& global,
                            const char* code, const char* name){
-  cout << name << endl;
-  cout << code << endl;
+  // cout << name << endl;
+  // cout << code << endl;
   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))));
 }
 
+Handle<Value> make_obj(const char* str){
+  Handle<String> src = String::New(str);
+  try{
+    Handle<Script> script = Script::Compile(src);
+    cout << "asdfafdsf" << endl;
+    return script->Run();
+  }catch(...){
+    cout << "asdfafdsf" << endl;
+    return v8::Undefined();
+  }
+}
+
+void call_and_print(Persistent<Object>& global,
+		    const char* name, const char* code,
+		    int args, Handle<Value>* argv){
+  stringstream code_;
+  code_ << name << " = " << code;
+  Local<Value> ret = create_func(global, code_.str().c_str(), name);
+  Persistent<Value> f = Persistent<Value>::New(ret);
+  if(! f->IsFunction()) throw std::runtime_error("not a function");
+  Handle<Value> result = ((Function*)(*f))->Call(global, args, argv);
+  String::AsciiValue ascii(result);
+  printf("%s\n", *ascii);
+}
+
 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();//NULL, global);
   Persistent<Object> global = Persistent<Object>::New(context->Global());
 
   // Create a string containing the JavaScript source code.
   //  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 };");
+  // ("[32, 234, 23/4, {asfd:\"asdf\"}, \"hello\"];");
+  //  ("{ someatom: 23, anotheratom: 3.3 };");
 
   Handle<Value> v0[1];
-  {
-    Handle<String> src = String::New("2344");
-    Handle<Script> script = Script::Compile(src);
-    v0[0] = script->Run();
-  }
+  v0[0] = make_obj("2345");
   //  global->Set("v0", 
-  const char* fun = "fun0 = function(i){ return i+1; }";
-  const char* fun1 = "fun1 = function(i){ return i-1; }";
-  Local<Value> ret = create_func(global, fun, "fun0");
-  Local<Value> ret1 = create_func(global, fun1, "fun1");
-  Persistent<Value> f = Persistent<Value>::New(ret);
+  call_and_print(global, "fun0", "function(i){ return i+1; }", 1, v0);
+  call_and_print(global, "fun1", "function(i){ return i-1; }", 1, v0);
 
-  cout << f->IsFunction() << endl;
-  Handle<Value> result = ((Function*)(*f))->Call(global, 1, v0);
-  String::AsciiValue ascii(result);
-  printf("%s\n", *ascii);
+  v0[0] = make_obj(" { asdf: 234, afsdasdf: [23,3,3,3] } ");
+  call_and_print(global, "fun2", "function(i){ return i['asdf']; }", 1, v0);
 
-  Persistent<Value> g = Persistent<Value>::New(ret1);
-  result = ((Function*)(*g))->Call(global, 1, v0);
-  String::AsciiValue ascii1(result);
-  printf("%s\n", *ascii1);
-
-  // Run the script to get the result.
-  //  Handle<Value> result = script->Run();
-  
   // Dispose the persistent context.
   context.Dispose();
-
-  // Convert the result to an ASCII string and print it.
   return 0;
 }