Skip to content

Commit

Permalink
Merge pull request #1910 from opal/elia/misc-improvements
Browse files Browse the repository at this point in the history
misc improvements
  • Loading branch information
elia committed Dec 11, 2018
2 parents 501c975 + ccf73fd commit e7cff26
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 30 deletions.
21 changes: 17 additions & 4 deletions lib/opal/compiler.rb
Expand Up @@ -270,16 +270,29 @@ def fragment(str, scope, sexp = nil)
def unique_temp(name)
name = name.to_s
if name && !name.empty?
name = "_#{name}"
.gsub('?', '$q')
.gsub('!', '$B')
name = name
.to_s
.gsub('<=>', '$lt_eq_gt')
.gsub('===', '$eq_eq_eq')
.gsub('==', '$eq_eq')
.gsub('=~', '$eq_tilde')
.gsub('!~', '$excl_tilde')
.gsub('!=', '$not_eq')
.gsub('<=', '$lt_eq')
.gsub('>=', '$gt_eq')
.gsub('=', '$eq')
.gsub('?', '$ques')
.gsub('!', '$excl')
.gsub('/', '$slash')
.gsub('%', '$percent')
.gsub('+', '$plus')
.gsub('-', '$minus')
.gsub('<', '$lt')
.gsub('>', '$gt')
.gsub(/[^\w\$]/, '$')
end
unique = (@unique += 1)
"TMP#{name}_#{unique}"
"#{'$' unless name.start_with?('$')}#{name}$#{unique}"
end

# Use the given helper
Expand Down
11 changes: 7 additions & 4 deletions opal/corelib/runtime.js
Expand Up @@ -496,7 +496,7 @@
return klass;
}

// Class doesnt exist, create a new one with given superclass...
// Class doesn't exist, create a new one with given superclass...

// Not specifying a superclass means we can assume it to be Object
if (superclass == null) {
Expand Down Expand Up @@ -543,6 +543,9 @@
Opal.allocate_module = function(name, constructor) {
var module = constructor;

if (name)
$defineProperty(constructor, 'displayName', name+'.constructor');

$defineProperty(module, '$$name', name);
$defineProperty(module, '$$const', {});
$defineProperty(module, '$$is_module', true);
Expand Down Expand Up @@ -1120,7 +1123,7 @@
// This method is used to setup a native constructor (e.g. Array), to have
// its prototype act like a normal Ruby class. Firstly, a new Ruby class is
// created using the native constructor so that its prototype is set as the
// target for th new class. Note: all bridged classes are set to inherit
// target for the new class. Note: all bridged classes are set to inherit
// from Object.
//
// Example:
Expand Down Expand Up @@ -1721,6 +1724,7 @@

// Define method on a module or class (see Opal.def).
Opal.defn = function(module, jsid, body) {
body.displayName = jsid;
body.$$owner = module;

var proto = module.prototype;
Expand Down Expand Up @@ -1752,8 +1756,7 @@
// Define a singleton method on the given object (see Opal.def).
Opal.defs = function(obj, jsid, body) {
if (obj.$$is_string || obj.$$is_number) {
// That's simply impossible
return;
throw Opal.TypeError.$new("can't define singleton");
}
Opal.defn(Opal.get_singleton_class(obj), jsid, body)
};
Expand Down
16 changes: 9 additions & 7 deletions spec/filters/unsupported/hash.rb
@@ -1,4 +1,7 @@
opal_unsupported_filter "Hash" do
fails "Hash#[]= duplicates and freezes string keys"
fails "Hash#[]= duplicates string keys using dup semantics" # TypeError: can't define singleton
fails "Hash#[]= raises a RuntimeError if called on a frozen instance"
fails "Hash#assoc only returns the first matching key-value pair for identity hashes"
fails "Hash#clear raises a RuntimeError if called on a frozen instance"
fails "Hash#compare_by_identity raises a RuntimeError on frozen hashes"
Expand All @@ -8,6 +11,8 @@
fails "Hash#delete_if raises a RuntimeError if called on a frozen instance"
fails "Hash#initialize is private"
fails "Hash#initialize raises a RuntimeError if called on a frozen instance"
fails "Hash#inspect does not raise if inspected result is not default external encoding" # Mock 'utf_16be' expected to receive 'inspect' exactly 1 times but received it 0 times
fails "Hash#inspect does not raise if inspected result is not default external encoding" # NoMethodError: undefined method `encode!' for "\"utf_16be あ\"":String
fails "Hash#inspect returns a tainted string if self is tainted and not empty"
fails "Hash#inspect returns an untrusted string if self is untrusted and not empty"
fails "Hash#keep_if raises a RuntimeError if called on a frozen instance"
Expand All @@ -24,20 +29,17 @@
fails "Hash#select! raises a RuntimeError if called on an empty frozen instance"
fails "Hash#shift raises a RuntimeError if called on a frozen instance"
fails "Hash#store duplicates and freezes string keys"
fails "Hash#store duplicates string keys using dup semantics" # TypeError: can't define singleton
fails "Hash#store raises a RuntimeError if called on a frozen instance"
fails "Hash#to_a returns a tainted array if self is tainted"
fails "Hash#to_a returns an untrusted array if self is untrusted"
fails "Hash#to_proc the returned proc raises ArgumentError if not passed exactly one argument"
fails "Hash#to_proc the returned proc passed as a block to instance_exec always retrieves the original hash's values"
fails "Hash#to_proc the returned proc raises ArgumentError if not passed exactly one argument"
fails "Hash#to_s does not raise if inspected result is not default external encoding" # Mock 'utf_16be' expected to receive 'inspect' exactly 1 times but received it 0 times
fails "Hash#to_s does not raise if inspected result is not default external encoding" # NoMethodError: undefined method `encode!' for "\"utf_16be あ\"":String
fails "Hash#to_s returns a tainted string if self is tainted and not empty"
fails "Hash#to_s returns an untrusted string if self is untrusted and not empty"
fails "Hash#update checks frozen status before coercing an object with #to_hash"
fails "Hash#update raises a RuntimeError on a frozen instance that is modified"
fails "Hash#update raises a RuntimeError on a frozen instance that would not be modified"
fails "Hash#[]= duplicates and freezes string keys"
fails "Hash#[]= raises a RuntimeError if called on a frozen instance"
fails "Hash#inspect does not raise if inspected result is not default external encoding" # Mock 'utf_16be' expected to receive 'inspect' exactly 1 times but received it 0 times
fails "Hash#inspect does not raise if inspected result is not default external encoding" # NoMethodError: undefined method `encode!' for "\"utf_16be あ\"":String
fails "Hash#to_s does not raise if inspected result is not default external encoding" # Mock 'utf_16be' expected to receive 'inspect' exactly 1 times but received it 0 times
fails "Hash#to_s does not raise if inspected result is not default external encoding" # NoMethodError: undefined method `encode!' for "\"utf_16be あ\"":String
end
19 changes: 10 additions & 9 deletions spec/filters/unsupported/kernel.rb
Expand Up @@ -7,8 +7,18 @@
fails "Kernel#Integer is a private method"
fails "Kernel#Integer returns a Fixnum or Bignum object"
fails "Kernel#String is a private method"
fails "Kernel#class returns the first non-singleton class" # TypeError: can't define singleton
fails "Kernel#clone preserves tainted state from the original"
fails "Kernel#clone preserves untrusted state from the original"
fails "Kernel#clone raises a TypeError for Symbol"
fails "Kernel#dup does not copy frozen state from the original"
fails "Kernel#dup preserves tainted state from the original"
fails "Kernel#dup preserves untrusted state from the original"
fails "Kernel#dup raises a TypeError for Symbol"
fails "Kernel#eql? is a public instance method"
fails "Kernel#format is a private method"
fails "Kernel#inspect returns an untrusted string if self is untrusted"
fails "Kernel#raise is a private method"
fails "Kernel#sleep accepts a Rational"
fails "Kernel#sleep is a private method"
fails "Kernel#sleep pauses execution indefinitely if not given a duration"
Expand All @@ -19,14 +29,5 @@
fails "Kernel.Float raises a TypeError if #to_f returns an Integer"
fails "Kernel.Integer calls to_i on Rationals"
fails "Kernel.Integer returns a Fixnum or Bignum object"
fails "Kernel#clone preserves tainted state from the original"
fails "Kernel#clone preserves untrusted state from the original"
fails "Kernel#clone raises a TypeError for Symbol"
fails "Kernel#dup does not copy frozen state from the original"
fails "Kernel#dup preserves tainted state from the original"
fails "Kernel#dup preserves untrusted state from the original"
fails "Kernel#dup raises a TypeError for Symbol"
fails "Kernel.fail is a private method"
fails "Kernel#raise is a private method"
fails "Kernel#inspect returns an untrusted string if self is untrusted"
end
3 changes: 1 addition & 2 deletions spec/filters/unsupported/string.rb
Expand Up @@ -251,13 +251,11 @@
fails "String#delete! returns nil if no modifications were made"
fails "String#delete_prefix returns a copy of the string, when the prefix isn't found" # Fails because "string".equal?("string") is always true
fails "String#delete_prefix! calls to_str on its argument"
fails "String#delete_prefix! calls to_str on its argument"
fails "String#delete_prefix! doesn't set $~"
fails "String#delete_prefix! removes the found prefix"
fails "String#delete_prefix! returns nil if no change is made"
fails "String#delete_suffix returns a copy of the string, when the suffix isn't found" # Fails because "string".equal?("string") is always true
fails "String#delete_suffix! calls to_str on its argument"
fails "String#delete_suffix! calls to_str on its argument"
fails "String#delete_suffix! doesn't set $~"
fails "String#delete_suffix! removes the found prefix"
fails "String#delete_suffix! returns nil if no change is made"
Expand All @@ -281,6 +279,7 @@
fails "String#downcase! sets the result String encoding to the source String encoding" # Expected #<Encoding:UTF-16LE> to be identical to #<Encoding:UTF-8>
fails "String#dump untrusts the result if self is untrusted"
fails "String#dup copies instance variables" # Strings are immutable, so they can't have ivars
fails "String#dup does not copy singleton methods" # TypeError: can't define singleton
fails "String#dup does not modify the original string when changing dupped string"
fails "String#each_char is unicode aware"
fails "String#each_line does not care if the string is modified while substituting"
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/builder_spec.rb
Expand Up @@ -76,14 +76,14 @@
expect(Opal::Config.compiler_options[:arity_check]).to eq(false)
builder = described_class.new
builder.build_str('def foo; end', 'foo')
expect(builder.to_s).not_to include('TMP_foo_1.$$parameters = []')
expect(builder.to_s).not_to include('$foo$1.$$parameters = []')

Opal::Config.arity_check_enabled = true
expect(Opal::Config.arity_check_enabled).to eq(true)
expect(Opal::Config.compiler_options[:arity_check]).to eq(true)
builder = described_class.new
builder.build_str('def foo; end', 'foo')
expect(builder.to_s).to include('TMP_foo_1.$$parameters = []')
expect(builder.to_s).to include('$foo$1.$$parameters = []')
end

describe '#missing_require_severity' do
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/simple_server_spec.rb
Expand Up @@ -47,10 +47,10 @@

it 'respects config set in Opal::Config' do
Opal::Config.arity_check_enabled = false
expect(get('/assets/console.js').body).not_to include('TMP_Console_clear_1.$$parameters = []')
expect(get('/assets/console.js').body).not_to include('$Console_clear$1.$$parameters = []')

Opal::Config.arity_check_enabled = true
self.app = described_class.new(main: 'console')
expect(get('/assets/console.js').body).to include('TMP_Console_clear_1.$$parameters = []')
expect(get('/assets/console.js').body).to include('$Console_clear$1.$$parameters = []')
end
end

0 comments on commit e7cff26

Please sign in to comment.