jsg 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.idea/.gitignore +8 -0
- data/.idea/jsg.iml +94 -0
- data/.idea/misc.xml +4 -0
- data/.idea/modules.xml +8 -0
- data/.idea/vcs.xml +6 -0
- data/.rspec +3 -0
- data/.rubocop.yml +8 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +35 -0
- data/Rakefile +12 -0
- data/exe/jsg +68 -0
- data/jsg.rb +193 -0
- data/lib/jsg/version.rb +5 -0
- data/sig/jsg.rbs +4 -0
- data/template/.gitignore +2 -0
- data/template/Gemfile +9 -0
- data/template/assets/browser.script.iife.js +3019 -0
- data/template/index.html.erb +68 -0
- data/template/serve.sh +1 -0
- data/template/src/main.rb +3 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 17be1d2e6749531713121d32c6a693369be433ad4b089b3a24680432c5fa7b58
|
4
|
+
data.tar.gz: 5cf7794b2fdcf5bbfa66776bf5295059a5a535b1f191a632f024a859bf72e384
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: eea66cabb0155161fdf1b0e7dd25d239b9f3a54750fc8bd33e9528c50ec305e6b0644afed29e9d9560476c5b8a73e0d279d30fedee40154a151ff5cbdabebbce
|
7
|
+
data.tar.gz: b1213c76694bea4ace6b52339a75b879ecfecfa792227e6025e3f91f69b14f2e20752944ecedda3c89cbbbc4b391a813c306a1fc0b64139d2335f30d11f34be6
|
data/.idea/.gitignore
ADDED
data/.idea/jsg.iml
ADDED
@@ -0,0 +1,94 @@
|
|
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
ADDED
data/.idea/modules.xml
ADDED
data/.idea/vcs.xml
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Andi
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Jsg
|
2
|
+
|
3
|
+
TODO: Delete this and the text below, and describe your gem
|
4
|
+
|
5
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/jsg`. To experiment with that code, run `bin/console` for an interactive prompt.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
10
|
+
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
12
|
+
|
13
|
+
$ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
14
|
+
|
15
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
|
+
|
17
|
+
$ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Development
|
24
|
+
|
25
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
26
|
+
|
27
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/jsg.
|
32
|
+
|
33
|
+
## License
|
34
|
+
|
35
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/exe/jsg
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
#require "jsg"
|
4
|
+
|
5
|
+
require "erb"
|
6
|
+
require "fileutils"
|
7
|
+
|
8
|
+
module JSG
|
9
|
+
# create a new project at project_name
|
10
|
+
def self.create_project(project_name)
|
11
|
+
base_path = File.expand_path("../template", __dir__)
|
12
|
+
destination = File.join(Dir.pwd, project_name)
|
13
|
+
|
14
|
+
if Dir.exist?(destination)
|
15
|
+
puts "Sorry #{destination} already exists, please choose other name or delete the folder"
|
16
|
+
return
|
17
|
+
end
|
18
|
+
FileUtils.mkdir_p(destination)
|
19
|
+
|
20
|
+
Dir.glob("#{base_path}/**/*").each do |file_path|
|
21
|
+
next if File.directory?(file_path)
|
22
|
+
|
23
|
+
p file_path
|
24
|
+
|
25
|
+
content = File.read(file_path)
|
26
|
+
result = if file_path.end_with?(".erb")
|
27
|
+
template = ERB.new(content)
|
28
|
+
template.result(binding)
|
29
|
+
else
|
30
|
+
content
|
31
|
+
end
|
32
|
+
|
33
|
+
relative_path = file_path.sub("#{base_path}/", "").gsub(/\.erb$/, "")
|
34
|
+
new_file_path = File.join(destination, relative_path)
|
35
|
+
FileUtils.mkdir_p(File.dirname(new_file_path))
|
36
|
+
File.write(new_file_path, result)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.build
|
41
|
+
system("bundle install")
|
42
|
+
# rbwasm --log-level debug build --ruby-version 3.3 --target wasm32-unknown-wasip1 --build-profile full
|
43
|
+
system("bundle exec rbwasm build -o assets/ruby-app.wasm")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
command = ARGV.shift # First argument is the command
|
48
|
+
|
49
|
+
case command
|
50
|
+
when "new"
|
51
|
+
#options = parse_options
|
52
|
+
project_name = ARGV.shift # Next argument should be the project name
|
53
|
+
|
54
|
+
if project_name
|
55
|
+
JSG.create_project(project_name)
|
56
|
+
JSG.build
|
57
|
+
else
|
58
|
+
puts "Error: Project name is required."
|
59
|
+
puts "For help, run: jsg new --help"
|
60
|
+
exit 1
|
61
|
+
end
|
62
|
+
when "build"
|
63
|
+
JSG.build
|
64
|
+
else
|
65
|
+
puts "Unknown command: #{command}"
|
66
|
+
puts "Use 'jsg new --help' for creating new projects."
|
67
|
+
exit 1
|
68
|
+
end
|
data/jsg.rb
ADDED
@@ -0,0 +1,193 @@
|
|
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
|
+
end
|
158
|
+
|
159
|
+
# Extending the JS module to include new class methods
|
160
|
+
module JS
|
161
|
+
extend JSGPatch::ClassMethods
|
162
|
+
|
163
|
+
end
|
164
|
+
|
165
|
+
module JSG
|
166
|
+
def self.window(*args)
|
167
|
+
JS.send(:global, *args)
|
168
|
+
end
|
169
|
+
|
170
|
+
def self.document(*args)
|
171
|
+
JS.global.send(:document, *args)
|
172
|
+
end
|
173
|
+
|
174
|
+
def self.querySelectorAll(*args)
|
175
|
+
document.querySelectorAll(*args)
|
176
|
+
end
|
177
|
+
|
178
|
+
singleton_class.alias_method :w, :window
|
179
|
+
singleton_class.alias_method :d, :document
|
180
|
+
singleton_class.alias_method :q, :querySelectorAll
|
181
|
+
|
182
|
+
def self.method_missing(method, *args, &block)
|
183
|
+
if JS.respond_to?(method)
|
184
|
+
JS.send(method, *args, &block)
|
185
|
+
else
|
186
|
+
super
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
def self.respond_to_missing?(method, include_private = false)
|
191
|
+
JS.respond_to?(method, include_private) || super
|
192
|
+
end
|
193
|
+
end
|
data/lib/jsg/version.rb
ADDED
data/sig/jsg.rbs
ADDED
data/template/.gitignore
ADDED