Created
August 16, 2010 21:11
-
-
Save cowboyd/527745 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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