Skip to content

Commit

Permalink
Initial commit of reworking runtime for method tables
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Oct 31, 2013
1 parent e2b91a2 commit 88ec514
Show file tree
Hide file tree
Showing 18 changed files with 295 additions and 667 deletions.
14 changes: 14 additions & 0 deletions Rakefile
Expand Up @@ -46,6 +46,20 @@ task :build_specs do
env.build
end

desc "Build just opal.js to build/"
task :opal_js do
Opal::Processor.arity_check_enabled = false
Opal::Processor.const_missing_enabled = false

env = Opal::Environment.new

Dir.mkdir 'build' unless File.directory? 'build'

src = env['opal'].to_s
File.open("build/opal.js", 'w+') { |f| f << src }
puts "build/opal.js"
end

desc "Build opal.js and opal-parser.js to build/"
task :dist do
Opal::Processor.arity_check_enabled = false
Expand Down
24 changes: 12 additions & 12 deletions corelib/array.rb
Expand Up @@ -2,7 +2,7 @@ class Array
include Enumerable

# Mark all javascript arrays as being valid ruby arrays
`def._isArray = true`
`self.prototype._isArray = true`

def self.[](*objects)
objects
Expand Down Expand Up @@ -617,17 +617,17 @@ def fill(*args, &block)
raise TypeError, "length invalid with range" if two

left = Opal.coerce_to one.begin, Integer, :to_int
`left += #@length` if `left < 0`
`left += self.length` if `left < 0`
raise RangeError, "#{one.inspect} out of range" if `left < 0`

right = Opal.coerce_to one.end, Integer, :to_int
`right += #@length` if `right < 0`
`right += self.length` if `right < 0`
`right += 1` unless one.exclude_end?

return self if `right <= left`
elsif one
left = Opal.coerce_to one, Integer, :to_int
`left += #@length` if `left < 0`
`left += self.length` if `left < 0`
left = 0 if `left < 0`

if two
Expand All @@ -637,11 +637,11 @@ def fill(*args, &block)

`right += left`
else
right = @length
right = self.length
end
else
left = 0
right = @length
right = self.length
end

if `right > 2147483648`
Expand All @@ -650,21 +650,21 @@ def fill(*args, &block)
raise ArgumentError, "argument too big"
end

if `left > #@length`
if `left > self.length`
%x{
for (var i = #@length; i < right; i++) {
for (var i = self.length; i < right; i++) {
self[i] = nil;
}
}
end

if `right > #@length`
@length = right
if `right > self.length`
self.length = right
end

if block
%x{
for (var length = #@length; left < right; left++) {
for (var length = self.length; left < right; left++) {
var value = block(left);
if (value === $breaker) {
Expand All @@ -676,7 +676,7 @@ def fill(*args, &block)
}
else
%x{
for (var length = #@length; left < right; left++) {
for (var length = self.length; left < right; left++) {
self[left] = #{obj};
}
}
Expand Down
6 changes: 3 additions & 3 deletions corelib/class.rb
Expand Up @@ -28,7 +28,7 @@ def self.new(sup = Object, &block)

def allocate
%x{
var obj = new self._alloc;
var obj = new self;
obj._id = Opal.uid();
return obj;
}
Expand All @@ -41,8 +41,8 @@ def new(*args, &block)
%x{
var obj = #{allocate};
obj.$initialize._p = block;
obj.$initialize.apply(obj, args);
obj.$m.initialize._p = block;
obj.$m.initialize.apply(null, [obj].concat(args));
return obj;
}
end
Expand Down
6 changes: 3 additions & 3 deletions corelib/encoding.rb
Expand Up @@ -89,7 +89,7 @@ def each_byte(string, &block)
def bytesize
bytes.length
end
end
end if false

Encoding.register "UTF-16LE" do
def each_byte(string, &block)
Expand All @@ -106,7 +106,7 @@ def each_byte(string, &block)
def bytesize
bytes.length
end
end
end if false

Encoding.register "ASCII-8BIT", aliases: ["BINARY"], ascii: true do
def each_byte(string, &block)
Expand All @@ -120,7 +120,7 @@ def each_byte(string, &block)
def bytesize
bytes.length
end
end
end if false

class String
`def.encoding = #{Encoding::UTF_16LE}`
Expand Down
6 changes: 3 additions & 3 deletions corelib/hash.rb
Expand Up @@ -7,7 +7,7 @@ class Hash
return arguments[0];
}
var hash = new Hash._alloc,
var hash = new Hash,
keys = [],
assocs = {};
Expand Down Expand Up @@ -49,7 +49,7 @@ class Hash
# function
%x{
var $hash2 = Opal.hash2 = function(keys, map) {
var hash = new Hash._alloc;
var hash = new Hash;
hash.keys = keys;
hash.map = map;
return hash;
Expand All @@ -64,7 +64,7 @@ def self.[](*objs)

def self.allocate
%x{
var hash = new self._alloc;
var hash = new self;
hash.map = {};
hash.keys = [];
return hash;
Expand Down
5 changes: 3 additions & 2 deletions corelib/kernel.rb
Expand Up @@ -475,12 +475,13 @@ def singleton_class
return self.__meta__;
}
var meta = new $opal.Class._alloc;
var meta = new $opal.Class;
meta._klass = $opal.Class;
self.__meta__ = meta;
// FIXME - is this right? (probably - methods defined on
// class' singleton should also go to subclasses?)
meta._proto = self.constructor.prototype;
meta.prototype = self;
meta.$m_tbl = self.$m;
meta._isSingleton = true;
meta.__inc__ = [];
meta._methods = [];
Expand Down
34 changes: 19 additions & 15 deletions corelib/module.rb
Expand Up @@ -47,10 +47,10 @@ def <(other)

def alias_method(newname, oldname)
%x{
self._proto['$' + newname] = self._proto['$' + oldname];
self.$m_tbl[newname] = self.$m_tbl[oldname];
if (self._methods) {
$opal.donate(self, ['$' + newname ])
$opal.donate(self, [newname])
}
}
self
Expand Down Expand Up @@ -102,8 +102,8 @@ def append_features(klass)
klass.__parent = iclass;
var donator = module._proto,
prototype = klass._proto,
var donator = module.$m_tbl,
prototype = klass.$m_tbl,
methods = module._methods;
for (var i = 0, length = methods.length; i < length; i++) {
Expand Down Expand Up @@ -137,18 +137,20 @@ def attr_accessor(*names)

def attr_reader(*names)
%x{
var proto = #{self}._proto, cls = #{self};
var proto = self.prototype, mtbl = self.$m_tbl, cls = self;
for (var i = 0, length = names.length; i < length; i++) {
(function(name) {
proto[name] = nil;
var func = function() { return this[name] };
var func = function(self) { return self[name] };
if (cls._isSingleton) {
proto.constructor.prototype['$' + name] = func;
// FIXME
//proto.constructor.prototype['$' + name] = func;
}
else {
proto['$' + name] = func;
$opal.donate(self, ['$' + name ]);
mtbl[name] = func;
$opal.donate(self, [name]);
}
})(names[i]);
}
Expand All @@ -159,18 +161,20 @@ def attr_reader(*names)

def attr_writer(*names)
%x{
var proto = #{self}._proto, cls = #{self};
var proto = self.prototype, mtbl = self.$m_tbl, cls = self;
for (var i = 0, length = names.length; i < length; i++) {
(function(name) {
proto[name] = nil;
var func = function(value) { return this[name] = value; };
var func = function(self, value) { return self[name] = value; };
if (cls._isSingleton) {
proto.constructor.prototype['$' + name + '='] = func;
// FIXME
// proto.constructor.prototype['$' + name + '='] = func;
}
else {
proto['$' + name + '='] = func;
$opal.donate(self, ['$' + name + '=']);
mtbl[name + '='] = func;
$opal.donate(self, [name + '=']);
}
})(names[i]);
}
Expand Down Expand Up @@ -449,7 +453,7 @@ def to_s
end

def undef_method(symbol)
`$opal.add_stub_for(#{self}._proto, "$" + symbol)`
`$opal.add_stub_for(#{self}.$m_tbl, symbol)`
self
end
end
2 changes: 1 addition & 1 deletion corelib/range.rb
Expand Up @@ -2,7 +2,7 @@ class Range
include Enumerable

%x{
Range._proto._isRange = true;
//Range._proto._isRange = true;
Opal.range = function(first, last, exc) {
var range = new Range._alloc;
Expand Down

0 comments on commit 88ec514

Please sign in to comment.