1
0
Fork 0
mirror of https://github.com/rubyjs/therubyracer synced 2023-03-27 23:21:42 -04:00
therubyracer/lib/v8/conversion/class.rb

118 lines
No EOL
2.9 KiB
Ruby

class V8::Conversion
module Class
include V8::Conversion::Code
def to_template
weakcell(:constructor) do
template = V8::C::FunctionTemplate::New(Constructor.new(self))
prototype = template.InstanceTemplate()
prototype.SetNamedPropertyHandler(Get, Set)
prototype.SetIndexedPropertyHandler(IGet, ISet)
if self != ::Object && superclass != ::Object && superclass != ::Class
template.Inherit(superclass.to_template)
end
template
end
end
class Constructor
include V8::Error::Protect
def initialize(cls)
@class = cls
end
def call(arguments)
arguments.extend Args
protect do
if arguments.linkage_call?
arguments.link
else
arguments.construct @class
end
end
return arguments.This()
end
module Args
def linkage_call?
self.Length() == 1 && self[0].IsExternal()
end
def link
context.link self[0].Value(), This()
end
def construct(cls)
context.link cls.new(*to_args), This()
end
def context
V8::Context.current
end
def to_args
args = ::Array.new(Length())
0.upto(args.length - 1) do |i|
args[i] = self[i]
end
return args
end
end
end
module Accessor
include V8::Error::Protect
def intercept(info, key, &block)
context = V8::Context.current
access = context.access
object = context.to_ruby(info.This())
handles_property = true
dontintercept = proc do
handles_property = false
end
protect do
result = block.call(context, access, object, context.to_ruby(key), dontintercept)
handles_property ? context.to_v8(result) : V8::C::Value::Empty
end
end
end
class Get
extend Accessor
def self.call(property, info)
intercept(info, property) do |context, access, object, key, dontintercept|
access.get(object, key, &dontintercept)
end
end
end
class Set
extend Accessor
def self.call(property, value, info)
intercept(info, property) do |context, access, object, key, dontintercept|
access.set(object, key, context.to_ruby(value), &dontintercept)
end
end
end
class IGet
extend Accessor
def self.call(property, info)
intercept(info, property) do |context, access, object, key, dontintercept|
access.iget(object, key, &dontintercept)
end
end
end
class ISet
extend Accessor
def self.call(property, value, info)
intercept(info, property) do |context, access, object, key, dontintercept|
access.iset(object, key, context.to_ruby(value), &dontintercept)
end
end
end
end
end