jsg 0.1.2 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +24 -0
- data/README.md +99 -14
- data/exe/jsg +3 -95
- data/lib/jsg/cli.rb +110 -0
- data/lib/jsg/version.rb +1 -1
- data/lib/jsg.rb +211 -0
- data/sig/jsg.rbs +1 -1
- data/template/Gemfile +6 -5
- data/template/assets/browser.script.iife.js +244 -145
- data/template/index.html.erb +6 -22
- data/template/src/main.rb +9 -2
- metadata +17 -26
- data/.idea/.gitignore +0 -8
- data/.idea/jsg.iml +0 -94
- data/.idea/misc.xml +0 -4
- data/.idea/modules.xml +0 -8
- data/.idea/vcs.xml +0 -6
- data/.rspec +0 -3
- data/.rubocop.yml +0 -8
- data/Rakefile +0 -12
- data/jsg.rb +0 -198
data/template/index.html.erb
CHANGED
|
@@ -39,30 +39,14 @@
|
|
|
39
39
|
<h1><%= project_name %></h1>
|
|
40
40
|
<div id="spinner" class="spinner"></div>
|
|
41
41
|
<script type="text/ruby" data-eval="async">
|
|
42
|
-
require
|
|
43
|
-
require
|
|
44
|
-
require 'jsg'
|
|
42
|
+
require "js/require_remote"
|
|
43
|
+
require "jsg"
|
|
45
44
|
|
|
46
|
-
|
|
47
|
-
$0 = File::basename(app_path, ".rb") if app_path
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
# This is only needed if you want to use require_relative instead of JSG build
|
|
51
|
-
module Kernel
|
|
52
|
-
alias original_require_relative require_relative
|
|
53
|
-
def require_relative(path)
|
|
54
|
-
JS::RequireRemote.instance.load(path)
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
$window = JSG.global
|
|
59
|
-
$d = $window.document
|
|
60
|
-
$d.getElementById("spinner").style.display = "none"
|
|
61
|
-
|
|
62
|
-
puts "test"
|
|
63
|
-
JS.global[:console].log("test")
|
|
64
|
-
JS.global.console.log("yes")
|
|
45
|
+
JSG.document.getElementById("spinner").style.display = "none"
|
|
65
46
|
|
|
47
|
+
# Loads src/main.rb from the web server. rbwasm build only packs Ruby
|
|
48
|
+
# and the gems into assets/ruby-app.wasm, not your own files.
|
|
49
|
+
JS::RequireRemote.instance.load("src/main")
|
|
66
50
|
</script>
|
|
67
51
|
</body>
|
|
68
52
|
</html>
|
data/template/src/main.rb
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
-
# Your code goes here
|
|
2
|
-
#
|
|
1
|
+
# Your code goes here. index.html loads this file from the web server with
|
|
2
|
+
# JS::RequireRemote, so reload the page after changing it. Only changes to
|
|
3
|
+
# the Gemfile need a new `jsg build`.
|
|
3
4
|
|
|
5
|
+
d = JSG.document
|
|
6
|
+
|
|
7
|
+
d.createElement("p").tap do |p|
|
|
8
|
+
p.innerText = "Hello from Ruby #{RUBY_VERSION}!"
|
|
9
|
+
d.body.appendChild(p)
|
|
10
|
+
end
|
metadata
CHANGED
|
@@ -1,44 +1,45 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: jsg
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1
|
|
4
|
+
version: 0.2.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Andi
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: exe
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
|
-
name:
|
|
13
|
+
name: js
|
|
15
14
|
requirement: !ruby/object:Gem::Requirement
|
|
16
15
|
requirements:
|
|
17
|
-
- - "
|
|
16
|
+
- - "~>"
|
|
18
17
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: '
|
|
18
|
+
version: '2.10'
|
|
20
19
|
type: :runtime
|
|
21
20
|
prerelease: false
|
|
22
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
22
|
requirements:
|
|
24
|
-
- - "
|
|
23
|
+
- - "~>"
|
|
25
24
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: '
|
|
25
|
+
version: '2.10'
|
|
27
26
|
- !ruby/object:Gem::Dependency
|
|
28
|
-
name:
|
|
27
|
+
name: ruby_wasm
|
|
29
28
|
requirement: !ruby/object:Gem::Requirement
|
|
30
29
|
requirements:
|
|
31
30
|
- - "~>"
|
|
32
31
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: '2.
|
|
32
|
+
version: '2.10'
|
|
34
33
|
type: :runtime
|
|
35
34
|
prerelease: false
|
|
36
35
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
36
|
requirements:
|
|
38
37
|
- - "~>"
|
|
39
38
|
- !ruby/object:Gem::Version
|
|
40
|
-
version: '2.
|
|
41
|
-
description:
|
|
39
|
+
version: '2.10'
|
|
40
|
+
description: The jsg command creates, builds and serves ruby.wasm browser projects.
|
|
41
|
+
require "jsg" adds property access, setters, predicates and Ruby type conversion
|
|
42
|
+
to the js gem's JS::Object.
|
|
42
43
|
email:
|
|
43
44
|
- largo@users.noreply.github.com
|
|
44
45
|
executables:
|
|
@@ -46,19 +47,12 @@ executables:
|
|
|
46
47
|
extensions: []
|
|
47
48
|
extra_rdoc_files: []
|
|
48
49
|
files:
|
|
49
|
-
- ".idea/.gitignore"
|
|
50
|
-
- ".idea/jsg.iml"
|
|
51
|
-
- ".idea/misc.xml"
|
|
52
|
-
- ".idea/modules.xml"
|
|
53
|
-
- ".idea/vcs.xml"
|
|
54
|
-
- ".rspec"
|
|
55
|
-
- ".rubocop.yml"
|
|
56
50
|
- CHANGELOG.md
|
|
57
51
|
- LICENSE.txt
|
|
58
52
|
- README.md
|
|
59
|
-
- Rakefile
|
|
60
53
|
- exe/jsg
|
|
61
|
-
- jsg.rb
|
|
54
|
+
- lib/jsg.rb
|
|
55
|
+
- lib/jsg/cli.rb
|
|
62
56
|
- lib/jsg/version.rb
|
|
63
57
|
- sig/jsg.rbs
|
|
64
58
|
- template/Gemfile
|
|
@@ -72,9 +66,7 @@ licenses:
|
|
|
72
66
|
metadata:
|
|
73
67
|
allowed_push_host: https://rubygems.org
|
|
74
68
|
homepage_uri: https://github.com/largo/jsg
|
|
75
|
-
|
|
76
|
-
changelog_uri: https://github.com/largo/jsg/CHANGELOG.md
|
|
77
|
-
post_install_message:
|
|
69
|
+
changelog_uri: https://github.com/largo/jsg/blob/main/CHANGELOG.md
|
|
78
70
|
rdoc_options: []
|
|
79
71
|
require_paths:
|
|
80
72
|
- lib
|
|
@@ -89,8 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
89
81
|
- !ruby/object:Gem::Version
|
|
90
82
|
version: '0'
|
|
91
83
|
requirements: []
|
|
92
|
-
rubygems_version:
|
|
93
|
-
signing_key:
|
|
84
|
+
rubygems_version: 4.0.20
|
|
94
85
|
specification_version: 4
|
|
95
86
|
summary: JSG helps setting up ruby.wasm projects and comes with a nicer syntax
|
|
96
87
|
test_files: []
|
data/.idea/.gitignore
DELETED
data/.idea/jsg.iml
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
-
<module type="RUBY_MODULE" version="4">
|
|
3
|
-
<component name="ModuleRunConfigurationManager">
|
|
4
|
-
<shared />
|
|
5
|
-
</component>
|
|
6
|
-
<component name="NewModuleRootManager">
|
|
7
|
-
<content url="file://$MODULE_DIR$">
|
|
8
|
-
<sourceFolder url="file://$MODULE_DIR$/features" isTestSource="true" />
|
|
9
|
-
<sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
|
|
10
|
-
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
|
|
11
|
-
</content>
|
|
12
|
-
<orderEntry type="inheritedJdk" />
|
|
13
|
-
<orderEntry type="sourceFolder" forTests="false" />
|
|
14
|
-
<orderEntry type="library" scope="PROVIDED" name="addressable (v2.8.6, asdf: 3.2.3) [gem]" level="application" />
|
|
15
|
-
<orderEntry type="library" scope="PROVIDED" name="ast (v2.4.2, asdf: 3.2.3) [gem]" level="application" />
|
|
16
|
-
<orderEntry type="library" scope="PROVIDED" name="base64 (v0.2.0, asdf: 3.2.3) [gem]" level="application" />
|
|
17
|
-
<orderEntry type="library" scope="PROVIDED" name="bundler (v2.5.9, asdf: 3.2.3) [gem]" level="application" />
|
|
18
|
-
<orderEntry type="library" scope="PROVIDED" name="capybara (v3.40.0, asdf: 3.2.3) [gem]" level="application" />
|
|
19
|
-
<orderEntry type="library" scope="PROVIDED" name="cgi (v0.4.1, asdf: 3.2.3) [gem]" level="application" />
|
|
20
|
-
<orderEntry type="library" scope="PROVIDED" name="diff-lcs (v1.5.1, asdf: 3.2.3) [gem]" level="application" />
|
|
21
|
-
<orderEntry type="library" scope="PROVIDED" name="erb (v4.0.2, asdf: 3.2.3) [gem]" level="application" />
|
|
22
|
-
<orderEntry type="library" scope="PROVIDED" name="js (v2.6.0, asdf: 3.2.3) [gem]" level="application" />
|
|
23
|
-
<orderEntry type="library" scope="PROVIDED" name="json (v2.7.2, asdf: 3.2.3) [gem]" level="application" />
|
|
24
|
-
<orderEntry type="library" scope="PROVIDED" name="language_server-protocol (v3.17.0.3, asdf: 3.2.3) [gem]" level="application" />
|
|
25
|
-
<orderEntry type="library" scope="PROVIDED" name="matrix (v0.4.2, asdf: 3.2.3) [gem]" level="application" />
|
|
26
|
-
<orderEntry type="library" scope="PROVIDED" name="mini_mime (v1.1.5, asdf: 3.2.3) [gem]" level="application" />
|
|
27
|
-
<orderEntry type="library" scope="PROVIDED" name="nokogiri (v1.16.4, asdf: 3.2.3) [gem]" level="application" />
|
|
28
|
-
<orderEntry type="library" scope="PROVIDED" name="parallel (v1.24.0, asdf: 3.2.3) [gem]" level="application" />
|
|
29
|
-
<orderEntry type="library" scope="PROVIDED" name="parser (v3.3.1.0, asdf: 3.2.3) [gem]" level="application" />
|
|
30
|
-
<orderEntry type="library" scope="PROVIDED" name="public_suffix (v5.0.5, asdf: 3.2.3) [gem]" level="application" />
|
|
31
|
-
<orderEntry type="library" scope="PROVIDED" name="racc (v1.7.3, asdf: 3.2.3) [gem]" level="application" />
|
|
32
|
-
<orderEntry type="library" scope="PROVIDED" name="rack (v2.2.9, asdf: 3.2.3) [gem]" level="application" />
|
|
33
|
-
<orderEntry type="library" scope="PROVIDED" name="rack-test (v2.1.0, asdf: 3.2.3) [gem]" level="application" />
|
|
34
|
-
<orderEntry type="library" scope="PROVIDED" name="rainbow (v3.1.1, asdf: 3.2.3) [gem]" level="application" />
|
|
35
|
-
<orderEntry type="library" scope="PROVIDED" name="rake (v13.2.1, asdf: 3.2.3) [gem]" level="application" />
|
|
36
|
-
<orderEntry type="library" scope="PROVIDED" name="regexp_parser (v2.9.0, asdf: 3.2.3) [gem]" level="application" />
|
|
37
|
-
<orderEntry type="library" scope="PROVIDED" name="rexml (v3.2.6, asdf: 3.2.3) [gem]" level="application" />
|
|
38
|
-
<orderEntry type="library" scope="PROVIDED" name="rspec (v3.13.0, asdf: 3.2.3) [gem]" level="application" />
|
|
39
|
-
<orderEntry type="library" scope="PROVIDED" name="rspec-core (v3.13.0, asdf: 3.2.3) [gem]" level="application" />
|
|
40
|
-
<orderEntry type="library" scope="PROVIDED" name="rspec-expectations (v3.13.0, asdf: 3.2.3) [gem]" level="application" />
|
|
41
|
-
<orderEntry type="library" scope="PROVIDED" name="rspec-mocks (v3.13.1, asdf: 3.2.3) [gem]" level="application" />
|
|
42
|
-
<orderEntry type="library" scope="PROVIDED" name="rspec-support (v3.13.1, asdf: 3.2.3) [gem]" level="application" />
|
|
43
|
-
<orderEntry type="library" scope="PROVIDED" name="rubocop (v1.63.4, asdf: 3.2.3) [gem]" level="application" />
|
|
44
|
-
<orderEntry type="library" scope="PROVIDED" name="rubocop-ast (v1.31.3, asdf: 3.2.3) [gem]" level="application" />
|
|
45
|
-
<orderEntry type="library" scope="PROVIDED" name="ruby-progressbar (v1.13.0, asdf: 3.2.3) [gem]" level="application" />
|
|
46
|
-
<orderEntry type="library" scope="PROVIDED" name="ruby_wasm (v2.6.0, asdf: 3.2.3) [gem]" level="application" />
|
|
47
|
-
<orderEntry type="library" scope="PROVIDED" name="rubyzip (v2.3.2, asdf: 3.2.3) [gem]" level="application" />
|
|
48
|
-
<orderEntry type="library" scope="PROVIDED" name="selenium-webdriver (v4.19.0, asdf: 3.2.3) [gem]" level="application" />
|
|
49
|
-
<orderEntry type="library" scope="PROVIDED" name="unicode-display_width (v2.5.0, asdf: 3.2.3) [gem]" level="application" />
|
|
50
|
-
<orderEntry type="library" scope="PROVIDED" name="websocket (v1.2.10, asdf: 3.2.3) [gem]" level="application" />
|
|
51
|
-
<orderEntry type="library" scope="PROVIDED" name="xpath (v3.2.0, asdf: 3.2.3) [gem]" level="application" />
|
|
52
|
-
</component>
|
|
53
|
-
<component name="RakeTasksCache-v2">
|
|
54
|
-
<option name="myRootTask">
|
|
55
|
-
<RakeTaskImpl id="rake">
|
|
56
|
-
<subtasks>
|
|
57
|
-
<RakeTaskImpl description="Build jsg-0.1.0.gem into the pkg directory" fullCommand="build" id="build" />
|
|
58
|
-
<RakeTaskImpl id="build">
|
|
59
|
-
<subtasks>
|
|
60
|
-
<RakeTaskImpl description="Generate SHA512 checksum if jsg-0.1.0.gem into the checksums directory" fullCommand="build:checksum" id="checksum" />
|
|
61
|
-
</subtasks>
|
|
62
|
-
</RakeTaskImpl>
|
|
63
|
-
<RakeTaskImpl description="Remove any temporary products" fullCommand="clean" id="clean" />
|
|
64
|
-
<RakeTaskImpl description="Remove any generated files" fullCommand="clobber" id="clobber" />
|
|
65
|
-
<RakeTaskImpl description="Build and install jsg-0.1.0.gem into system gems" fullCommand="install" id="install" />
|
|
66
|
-
<RakeTaskImpl id="install">
|
|
67
|
-
<subtasks>
|
|
68
|
-
<RakeTaskImpl description="Build and install jsg-0.1.0.gem into system gems without network access" fullCommand="install:local" id="local" />
|
|
69
|
-
</subtasks>
|
|
70
|
-
</RakeTaskImpl>
|
|
71
|
-
<RakeTaskImpl description="Create tag v0.1.0 and build and push jsg-0.1.0.gem to TODO: Set to your gem server 'https://example.com'" fullCommand="release[remote]" id="release[remote]" />
|
|
72
|
-
<RakeTaskImpl description="Run RuboCop" fullCommand="rubocop" id="rubocop" />
|
|
73
|
-
<RakeTaskImpl id="rubocop">
|
|
74
|
-
<subtasks>
|
|
75
|
-
<RakeTaskImpl description="Autocorrect RuboCop offenses (only when it's safe)" fullCommand="rubocop:autocorrect" id="autocorrect" />
|
|
76
|
-
<RakeTaskImpl description="Autocorrect RuboCop offenses (safe and unsafe)" fullCommand="rubocop:autocorrect_all" id="autocorrect_all" />
|
|
77
|
-
<RakeTaskImpl description="" fullCommand="rubocop:auto_correct" id="auto_correct" />
|
|
78
|
-
</subtasks>
|
|
79
|
-
</RakeTaskImpl>
|
|
80
|
-
<RakeTaskImpl description="Run RSpec code examples" fullCommand="spec" id="spec" />
|
|
81
|
-
<RakeTaskImpl description="" fullCommand="default" id="default" />
|
|
82
|
-
<RakeTaskImpl description="" fullCommand="release" id="release" />
|
|
83
|
-
<RakeTaskImpl id="release">
|
|
84
|
-
<subtasks>
|
|
85
|
-
<RakeTaskImpl description="" fullCommand="release:guard_clean" id="guard_clean" />
|
|
86
|
-
<RakeTaskImpl description="" fullCommand="release:rubygem_push" id="rubygem_push" />
|
|
87
|
-
<RakeTaskImpl description="" fullCommand="release:source_control_push" id="source_control_push" />
|
|
88
|
-
</subtasks>
|
|
89
|
-
</RakeTaskImpl>
|
|
90
|
-
</subtasks>
|
|
91
|
-
</RakeTaskImpl>
|
|
92
|
-
</option>
|
|
93
|
-
</component>
|
|
94
|
-
</module>
|
data/.idea/misc.xml
DELETED
data/.idea/modules.xml
DELETED
data/.idea/vcs.xml
DELETED
data/.rspec
DELETED
data/.rubocop.yml
DELETED
data/Rakefile
DELETED
data/jsg.rb
DELETED
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require_relative "jsg/version"
|
|
4
|
-
require 'js'
|
|
5
|
-
|
|
6
|
-
module JSGPatch
|
|
7
|
-
def self.included(base)
|
|
8
|
-
base.extend ClassMethods
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
module ClassMethods
|
|
12
|
-
# JS.try_convert_to_rb(obj) -> Ruby Object or JS::Object
|
|
13
|
-
#
|
|
14
|
-
# Try to convert the given object to a Ruby Datatype using to_rb
|
|
15
|
-
# method. Returns the parameter as JS::Object if the object cannot be converted.
|
|
16
|
-
def try_convert_to_rb(obj)
|
|
17
|
-
return obj.to_rb
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
# Instance methods to patch or add
|
|
22
|
-
def to_a(convertTypes: true)
|
|
23
|
-
as_array = JS.global[:Array].from(self)
|
|
24
|
-
Array.new(as_array[:length].to_i) do |index|
|
|
25
|
-
item = as_array[index]
|
|
26
|
-
convertTypes && item.respond_to?(:to_rb) ? item.to_rb : item
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
def to_rb
|
|
31
|
-
return nil if self == JS::Null
|
|
32
|
-
case self.typeof
|
|
33
|
-
when "number" then self.to_f
|
|
34
|
-
when "string" then self.to_s
|
|
35
|
-
when "boolean" then self == JS::True
|
|
36
|
-
when "symbol" then self.to_sym
|
|
37
|
-
when "bigint" then self.to_i
|
|
38
|
-
when "object"
|
|
39
|
-
if self.isJSArray
|
|
40
|
-
self.to_a
|
|
41
|
-
else
|
|
42
|
-
self
|
|
43
|
-
end
|
|
44
|
-
else
|
|
45
|
-
self
|
|
46
|
-
end
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
def isJSArray
|
|
50
|
-
JS.global[:Array].isArray(self) == JS::True
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
def typeof?(type)
|
|
54
|
-
self.typeof == type.to_s
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
# Support self == true instead of self == JS:True
|
|
59
|
-
alias_method :orig_eq, :==
|
|
60
|
-
def ==(other)
|
|
61
|
-
if other.equal? true
|
|
62
|
-
return orig_eq(JS::True)
|
|
63
|
-
elsif other.equal? false
|
|
64
|
-
return orig_eq(JS::False)
|
|
65
|
-
elsif other.equal? nil
|
|
66
|
-
return orig_eq(JS::Null) || orig_eq(JS::Undefined)
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
orig_eq(other)
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
def self.static_to_rb(object)
|
|
74
|
-
return nil if self[sym] == JS::Null
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
def each(&block)
|
|
78
|
-
if block_given?
|
|
79
|
-
if self.isJSArray
|
|
80
|
-
self.to_a.each(&block)
|
|
81
|
-
else
|
|
82
|
-
Array.new(__props).each(&block)
|
|
83
|
-
end
|
|
84
|
-
else
|
|
85
|
-
to_enum(:each)
|
|
86
|
-
end
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
def nil?
|
|
90
|
-
return self == JS::Null
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
def undefined?
|
|
94
|
-
return self == JS::Undefined
|
|
95
|
-
end
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
def __props
|
|
99
|
-
props = []
|
|
100
|
-
current_obj = self
|
|
101
|
-
begin
|
|
102
|
-
current_props = JS.global[:Object].getOwnPropertyNames(current_obj).to_a
|
|
103
|
-
props.concat(current_props)
|
|
104
|
-
current_obj = JS.global[:Object].getPrototypeOf(current_obj)
|
|
105
|
-
end while current_obj != JS::Null && current_obj != JS::Undefined
|
|
106
|
-
props.compact.map(&:to_sym)
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
def method_missing(sym, *args, &block)
|
|
110
|
-
return super(sym, *args, &block) if self === JS::Null
|
|
111
|
-
sym_str = sym.to_s
|
|
112
|
-
sym = sym_str[0..-2].to_sym if sym_str.end_with?("?") or sym_str.end_with?("=")
|
|
113
|
-
if sym_str.end_with?("?")
|
|
114
|
-
# When a JS method is called with a ? suffix, it is treated as a predicate method,
|
|
115
|
-
# and the return value is converted to a Ruby boolean value automatically.
|
|
116
|
-
if self[sym]&.typeof?(:function)
|
|
117
|
-
return self.call(sym, *args, &block) == JS::True
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
return self[sym] == JS::True
|
|
121
|
-
end
|
|
122
|
-
|
|
123
|
-
if sym_str.end_with?("=")
|
|
124
|
-
if args[0].respond_to?(:to_js)
|
|
125
|
-
return self[sym] = args[0].to_js
|
|
126
|
-
end
|
|
127
|
-
|
|
128
|
-
return self[sym] = args[0]
|
|
129
|
-
end
|
|
130
|
-
|
|
131
|
-
if self[sym]&.typeof?(:function) # Todo: What do we do when we want to copy functions around?
|
|
132
|
-
begin
|
|
133
|
-
result = self.call(sym, *args, &block)
|
|
134
|
-
if result.typeof?(:boolean) # fixes if searchParams.has("locations")
|
|
135
|
-
return result == JS::True
|
|
136
|
-
else
|
|
137
|
-
return result.to_rb if result.respond_to?(:to_rb)
|
|
138
|
-
return result
|
|
139
|
-
end
|
|
140
|
-
rescue
|
|
141
|
-
return self[sym] # TODO: this is necessary in cases like JS.global[:URLSearchParams]
|
|
142
|
-
end
|
|
143
|
-
end
|
|
144
|
-
|
|
145
|
-
if self[sym]&.typeof?(:undefined) == false and self[sym].respond_to?(:to_rb)
|
|
146
|
-
return self[sym].to_rb
|
|
147
|
-
end
|
|
148
|
-
|
|
149
|
-
return super(sym, *args, &block)
|
|
150
|
-
end
|
|
151
|
-
|
|
152
|
-
end
|
|
153
|
-
|
|
154
|
-
# Applying the JSG module to JS::Object to patch existing methods and add new ones
|
|
155
|
-
class JS::Object
|
|
156
|
-
include JSGPatch
|
|
157
|
-
|
|
158
|
-
end
|
|
159
|
-
|
|
160
|
-
# Extending the JS module to include new class methods
|
|
161
|
-
module JS
|
|
162
|
-
extend JSGPatch::ClassMethods
|
|
163
|
-
|
|
164
|
-
end
|
|
165
|
-
|
|
166
|
-
module JSG
|
|
167
|
-
class Error < StandardError; end
|
|
168
|
-
|
|
169
|
-
def self.window(*args)
|
|
170
|
-
JS.send(:global, *args)
|
|
171
|
-
end
|
|
172
|
-
|
|
173
|
-
def self.document(*args)
|
|
174
|
-
JS.global.send(:document, *args)
|
|
175
|
-
end
|
|
176
|
-
|
|
177
|
-
def self.querySelectorAll(*args)
|
|
178
|
-
document.querySelectorAll(*args)
|
|
179
|
-
end
|
|
180
|
-
|
|
181
|
-
singleton_class.alias_method :w, :window
|
|
182
|
-
singleton_class.alias_method :d, :document
|
|
183
|
-
singleton_class.alias_method :q, :querySelectorAll
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
def self.method_missing(method, *args, &block)
|
|
188
|
-
if JS.respond_to?(method)
|
|
189
|
-
JS.send(method, *args, &block)
|
|
190
|
-
else
|
|
191
|
-
super
|
|
192
|
-
end
|
|
193
|
-
end
|
|
194
|
-
|
|
195
|
-
def self.respond_to_missing?(method, include_private = false)
|
|
196
|
-
JS.respond_to?(method, include_private) || super
|
|
197
|
-
end
|
|
198
|
-
end
|