Skip to content

Instantly share code, notes, and snippets.

@cowboyd
Created August 16, 2010 21:11
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 cowboyd/527745 to your computer and use it in GitHub Desktop.
Save cowboyd/527745 to your computer and use it in GitHub Desktop.
require 'v8'
include V8::C
print = FunctionTemplate::New() do |arguments|
for i in 0..arguments.Length() - 1
print case arg = arguments[i]
when TrueClass,FalseClass, nil
arg
else
arg.ToString().AsciiValue()
end
end
puts
end
template = ObjectTemplate::New()
template.SetCallAsFunctionHandler() do |arguments|
V8::C::Object::New()
end
context = Context::New()
context.Enter()
context.Global().Set("Foo", template.NewInstance())
context.Global().Set("print", print.GetFunction())
TryCatch.try do |try_catch|
script = Script::Compile(<<-JS, "<eval>")
Foo.prototype = {bar: 'baz'}
var f = new Foo()
print("foo: ", Foo) //=> [object Object], should be function Foo() {}.
print("typeof Foo: ", typeof Foo) //=> 'object', should be 'function'
print("f.constructor == Foo: ", f.constructor == Foo) //=> false, should be true
print("f.bar: ", f.bar) //=> undefined, should be 'baz'
print("f instanceof Foo", f instanceof Foo) //=> TypeError: Expecting a function in instanceof check, but got #<an Object>
JS
result = script.Run()
if try_catch.HasCaught()
msg = try_catch.Message()
puts msg.Get().AsciiValue() + ": #{msg.GetLineNumber()}"
end
end
context.Exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment