Skip to content

Instantly share code, notes, and snippets.

@laverdet
Created September 4, 2012 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laverdet/3619179 to your computer and use it in GitHub Desktop.
Save laverdet/3619179 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <v8.h>
using namespace std;
using namespace v8;
// simple print utility; mostly lifted from shell.cc
Handle<Value> Print(const Arguments& args) {
bool first = true;
for (int i = 0; i < args.Length(); i++) {
HandleScope scope;
if (first) {
first = false;
} else {
printf(" ");
}
String::Utf8Value str(args[i]);
printf("%s", *str);
}
printf("\n");
fflush(stdout);
return Undefined();
}
int main() {
const char* src = "\
(function F(ii) {\
print(ii, ii.toString());\
F(ii + 1);\
})(0);";
HandleScope scope;
Persistent<Context> context = Context::New();
Context::Scope context_scope(context);
context->Global()->Set(String::New("print"), FunctionTemplate::New(Print)->GetFunction());
Handle<Script> script = Script::Compile(String::New(src));
TryCatch try_catch;
Handle<Value> result = script->Run();
if (try_catch.HasCaught()) {
String::Utf8Value stack(try_catch.StackTrace());
cerr <<"caught:\n";
cerr <<*stack;
} else {
cout <<"success\n";
}
return 0;
}
@laverdet
Copy link
Author

laverdet commented Sep 4, 2012

./a.out | tail -n50
15690 15690
15691 15691
15692 15692
15693 15693
15694 15694
15695 15695
15696 15696
15697 15697
15698 15698
15699 15699
15700 15700
15701 15701
15702 15702
15703 15703
15704 15704
15705 15705
15706 15706
15707 15707
15708 15708
15709 15709
(null) 15710
(null) 15711
(null) 15712
(null) 15713
(null) 15714
(null) 15715
(null) 15716
(null) 15717
(null) 15718
(null) 15719
(null) 15720
(null) 15721
(null) 15722
(null) 15723
(null) 15724
(null) 15725
(null) 15726
(null) 15727
(null) 15728
(null) 15729
(null) 15730
(null) 15731
(null) 15732
(null) 15733
(null) 15734
(null) 15735
(null) 15736
(null) 15737
(null) 15738
(null) 15739
caught:
undefined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment