Skip to content

Commit

Permalink
Deprecate include Native, use Native::Wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
elia committed Feb 20, 2018
1 parent 31e7b26 commit 21713a4
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Whitespace conventions:
### Deprecated

- The CLI `--server-port 1234` option is now deprecated in favor of using `--runner-options='{"port": 1234}'`
- Including `::Native` is now deprecated because it generates conflicts with core classes in constant lookups (both `Native::Object` and `Native::Array` exist). Instead `Native::Werapper` should be used.


### Removed
Expand Down
6 changes: 3 additions & 3 deletions spec/opal/stdlib/native/alias_native_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
describe "Native.alias_native" do
it "refers to an attribute on @native" do
Class.new {
include Native
include Native::Wrapper

alias_native :a, :a
}.new(`{ a: 2 }`).a.should == 2
end

it "refers to an attribute on @native and calls it if it's a function" do
Class.new {
include Native
include Native::Wrapper

alias_native :a, :a
}.new(`{ a: function() { return 42; } }`).a.should == 42
end

it "defaults old to new" do
Class.new {
include Native
include Native::Wrapper

alias_native :a
}.new(`{ a: 42 }`).a.should == 42
Expand Down
8 changes: 8 additions & 0 deletions spec/opal/stdlib/native/deprecated_include_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'native'

describe "Native inclusion" do
it "is deprecated" do
Native.should_receive(:warn).with("Including ::Native is deprecated. Please include Native::Wrapper instead.")
Class.new { include Native }
end
end
4 changes: 2 additions & 2 deletions spec/opal/stdlib/native/native_reader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
describe "Native.native_reader" do
it "refers to an attribute on @native" do
Class.new {
include Native
include Native::Wrapper

native_reader :a
}.new(`{ a: 2 }`).a.should == 2
end

it "uses multiple names" do
n = Class.new {
include Native
include Native::Wrapper

native_reader :a, :b
}.new(`{ a: 2, b: 3 }`)
Expand Down
4 changes: 2 additions & 2 deletions spec/opal/stdlib/native/native_writer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe "Native.native_writer" do
it "refers to an attribute on @native" do
n = Class.new {
include Native
include Native::Wrapper

native_reader :a
native_writer :a
Expand All @@ -15,7 +15,7 @@

it "supports multiple names" do
n = Class.new {
include Native
include Native::Wrapper

native_reader :a, :b
native_writer :a, :b
Expand Down
2 changes: 1 addition & 1 deletion stdlib/buffer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'buffer/view'

class Buffer
include Native
include Native::Wrapper

def self.supported?
not $$[:ArrayBuffer].nil?
Expand Down
2 changes: 1 addition & 1 deletion stdlib/buffer/array.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Buffer

class Array
include Native
include Native::Wrapper

def self.for(bits, type)
$$["#{Buffer.name_for bits, type}Array"]
Expand Down
2 changes: 1 addition & 1 deletion stdlib/buffer/view.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Buffer

class View
include Native
include Native::Wrapper

def self.supported?
not $$[:DataView].nil?
Expand Down
2 changes: 1 addition & 1 deletion stdlib/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# @see https://developer.mozilla.org/en-US/docs/Web/API/console
class Console
include Native
include Native::Wrapper

# Clear the console.
def clear
Expand Down
32 changes: 20 additions & 12 deletions stdlib/native.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,21 +187,29 @@ def native_accessor(*names)
end
end

def self.included(klass)
klass.extend Helpers
end
module Wrapper
def initialize(native)
unless ::Kernel.native?(native)
::Kernel.raise ArgumentError, "#{native.inspect} isn't native"
end

@native = native
end

def initialize(native)
unless ::Kernel.native?(native)
::Kernel.raise ArgumentError, "#{native.inspect} isn't native"
# Returns the internal native JavaScript value
def to_n
@native
end

@native = native
def self.included(klass)
klass.extend Helpers
end
end

# Returns the internal native JavaScript value
def to_n
@native
def self.included(base)
warn "Including ::Native is deprecated. Please include Native::Wrapper instead."
base.include Wrapper
base.extend Helpers
end
end

Expand Down Expand Up @@ -245,7 +253,7 @@ def Array(object, *args, &block)
end

class Native::Object < BasicObject
include ::Native
include ::Native::Wrapper

def ==(other)
`#@native === #{::Native.try_convert(other)}`
Expand Down Expand Up @@ -355,7 +363,7 @@ def inspect
end

class Native::Array
include Native
include Native::Wrapper
include Enumerable

def initialize(native, options = {}, &block)
Expand Down

0 comments on commit 21713a4

Please sign in to comment.