Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 802b5071c6d9
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: cfc5833d5daa
Choose a head ref
  • 2 commits
  • 4 files changed
  • 2 contributors

Commits on Sep 7, 2015

  1. Copy the full SHA
    c28b7ab View commit details
  2. Merge pull request #1094 from iliabylich/store-struct-attributes-in-h…

    …ash-instead-of-ivars
    
    Store Struct attributes in hash instead of instance variables.
    meh committed Sep 7, 2015
    Copy the full SHA
    cfc5833 View commit details
Showing with 31 additions and 23 deletions.
  1. +6 −0 opal/corelib/helpers.rb
  2. +25 −8 opal/corelib/struct.rb
  3. +0 −1 spec/filters/bugs/struct.rb
  4. +0 −14 stdlib/native.rb
6 changes: 6 additions & 0 deletions opal/corelib/helpers.rb
Original file line number Diff line number Diff line change
@@ -113,4 +113,10 @@ def self.instance_variable_name!(name)

name
end

def self.valid_method_name?(method_name)
method_name = Opal.coerce_to!(method_name, String, :to_str)

`/^[a-zA-Z_][a-zA-Z0-9_]*?$/.test(method_name)`
end
end
33 changes: 25 additions & 8 deletions opal/corelib/struct.rb
Original file line number Diff line number Diff line change
@@ -15,6 +15,17 @@ def self.new(name = undefined, *args, &block)
args.each { |arg| define_struct_attribute arg }

instance_eval(&block) if block

class << self
def new(*args)
instance = allocate
`#{instance}.$$data = {};`
instance.initialize(*args)
instance
end

alias [] new
end
}
end
end
@@ -26,7 +37,15 @@ def self.define_struct_attribute(name)

members << name

attr_accessor name
if Opal.valid_method_name?(name)
define_method name do
self[name]
end

define_method "#{name}=" do |value|
self[name] = value
end
end
end

def self.members
@@ -45,13 +64,9 @@ def self.inherited(klass)
}
end

class << self
alias [] new
end

def initialize(*args)
members.each_with_index {|name, index|
instance_variable_set "@#{name}", args[index]
self[name] = args[index]
}
end

@@ -71,7 +86,8 @@ def [](name)
raise TypeError, "no implicit conversion of #{name.class} into Integer"
end

instance_variable_get "@#{name}"
name = Opal.coerce_to!(name, String, :to_str)
`self.$$data[name]`
end

def []=(name, value)
@@ -86,7 +102,8 @@ def []=(name, value)
raise TypeError, "no implicit conversion of #{name.class} into Integer"
end

instance_variable_set "@#{name}", value
name = Opal.coerce_to!(name, String, :to_str)
`self.$$data[name] = value`
end

def each
1 change: 0 additions & 1 deletion spec/filters/bugs/struct.rb
Original file line number Diff line number Diff line change
@@ -19,5 +19,4 @@
fails "Struct.new fails with too many arguments"
fails "Struct.new raises a TypeError if object doesn't respond to to_sym"
fails "Struct.new raises a TypeError if object is not a Symbol"
fails "Struct#[] returns attribute names that contain hyphens"
end
14 changes: 0 additions & 14 deletions stdlib/native.rb
Original file line number Diff line number Diff line change
@@ -405,20 +405,6 @@ def to_n
end

class Struct
def initialize(*args)
if args.length == 1 && native?(args[0])
object = args[0]

members.each {|name|
instance_variable_set "@#{name}", Native(`#{object}[#{name}]`)
}
else
members.each_with_index {|name, index|
instance_variable_set "@#{name}", args[index]
}
end
end

def to_n
result = `{}`