Skip to content

Instantly share code, notes, and snippets.

@drnic
Created September 3, 2008 01:28
Show Gist options
  • Save drnic/8514 to your computer and use it in GitHub Desktop.
Save drnic/8514 to your computer and use it in GitHub Desktop.

Hello World example for V8

It includes generic Rakefile for other projects

Usage

#include <v8.h>
using namespace v8;
int main(int argc, char* argv[]) {
// Create a stack-allocated handle scope.
HandleScope handle_scope;
// Create a new context.
Handle<Context> context = Context::New();
// Enter the created context for compiling and
// running the hello world script.&nbsp;
Context::Scope context_scope(context);
// Create a string containing the JavaScript source code.
Handle<String> source = String::New("'Hello' + ', World!'");
// Compile the source code.
Handle<Script> script = Script::Compile(source);
// Run the script to get the result.
Handle<Value> result = script->Run();
// Convert the result to an ASCII string and print it.
String::AsciiValue ascii(result);
printf("%s\n", *ascii);
return 0;
}
require "rubygems"
require "rake"
require "rake/clean"
PROG = "hello_world"
SRC = FileList['**/*.cpp']
V8 = ENV['V8']
CLEAN.include(PROG)
task :default => [:build, :run]
task :build => [PROG]
task :run => [PROG] do
sh "./#{PROG}"
end
file PROG => SRC do
sh "g++ -o #{PROG} #{SRC} -I#{V8}/include #{V8}/libv8.a -lpthread"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment