File tree Expand file tree Collapse file tree 4 files changed +35
-45
lines changed
packages/npm-packages/ruby-wasm-wasi Expand file tree Collapse file tree 4 files changed +35
-45
lines changed Original file line number Diff line number Diff line change 1
1
require "singleton"
2
+ require "js"
2
3
require_relative "./url_resolver"
3
4
4
5
module JS
@@ -11,7 +12,11 @@ def initialize
11
12
set_base_url
12
13
end
13
14
14
- def require_relative ( relative_feature )
15
+ # Load the given feature from remote.
16
+ # The use_maps parameter is not used.
17
+ # This is a parameter to keep the method name unchanged when supporting require methods.
18
+ # Maps like import maps will be used.
19
+ def load ( relative_feature , use_maps : false )
15
20
location = get_location ( relative_feature )
16
21
response = JS . global . fetch ( location . url ) . await
17
22
Original file line number Diff line number Diff line change 1
1
< html >
2
2
< script src ="
https://cdn.jsdelivr.net/npm/[email protected] /dist/browser.script.iife.js "
> </ script >
3
3
< script type ="text/ruby " data-eval ="async ">
4
+ # Patch require_relative to load from remote
5
+ require 'js/require_remote'
6
+
7
+ module Kernel
8
+ alias original_require_relative require_relative
9
+
10
+ # The require_relative may be used in the embedded Gem .
11
+ # First try to load from the built - in filesystem , and if that fails ,
12
+ # load from the URL .
13
+ def require_relative ( path )
14
+ caller_path = caller_locations ( 1 , 1 ) . first . absolute_path || ''
15
+ dir = File . dirname ( caller_path )
16
+ file = File . absolute_path ( path , dir )
17
+
18
+ original_require_relative ( file )
19
+ rescue LoadError
20
+ JS::RequireRemote . instance . load ( path )
21
+ end
22
+ end
23
+
24
+ # The above patch does not break the original require_relative
25
+ require 'csv'
26
+ csv = CSV . new "foo\nbar\n"
27
+
28
+ # Load the main script
4
29
require_relative 'main'
5
30
</ script >
6
31
</ html >
Original file line number Diff line number Diff line change @@ -12,8 +12,6 @@ export const main = async (pkg: { name: string; version: string }) => {
12
12
13
13
globalThis . rubyVM = vm ;
14
14
15
- patchRequireRelative ( vm ) ;
16
-
17
15
// Wait for the text/ruby script tag to be read.
18
16
// It may take some time to read ruby+stdlib.wasm
19
17
// and DOMContentLoaded has already been fired.
@@ -96,28 +94,3 @@ const compileWebAssemblyModule = async function (
96
94
return WebAssembly . compileStreaming ( response ) ;
97
95
}
98
96
} ;
99
-
100
- const patchRequireRelative = ( vm : RubyVM ) => {
101
- const patch = `
102
- require 'js'
103
- require 'js/require_remote'
104
-
105
- module Kernel
106
- alias original_require_relative require_relative
107
-
108
- # The require_relative may be used in the embedded Gem.
109
- # First try to load from the built-in filesystem, and if that fails,
110
- # load from the URL.
111
- def require_relative(path)
112
- caller_path = caller_locations(1, 1).first.absolute_path || ''
113
- dir = File.dirname(caller_path)
114
- file = File.absolute_path(path, dir)
115
-
116
- original_require_relative(file)
117
- rescue LoadError
118
- JS::RequireRemote.instance.require_relative(path)
119
- end
120
- end
121
- ` ;
122
- vm . eval ( patch ) ;
123
- } ;
Original file line number Diff line number Diff line change @@ -61,22 +61,8 @@ if (!process.env.RUBY_NPM_PACKAGE_ROOT) {
61
61
} ) ;
62
62
} ) ;
63
63
64
- test . describe ( "require_relative" , ( ) => {
65
- test ( "patch does not break original require_relative" , async ( { page } ) => {
66
- const resolve = await resolveBinding ( page , "checkResolved" ) ;
67
- await page . setContent ( `
68
- <script src="https://cdn.jsdelivr.net/npm/ruby-head-wasm-wasi@latest/dist/browser.script.iife.js">
69
- </script>
70
- <script type="text/ruby" data-eval="async">
71
- require 'csv'
72
- csv = CSV.new "foo\nbar\n"
73
- JS.global.checkResolved csv.first
74
- </script>
75
- ` ) ;
76
- expect ( await resolve ( ) ) . toStrictEqual ( [ "foo" ] ) ;
77
- } ) ;
78
-
79
- test ( "require_relative throws error when gem is not found" , async ( {
64
+ test . describe ( 'JS::RequireRemote#load' , ( ) => {
65
+ test ( "JS::RequireRemote#load throws error when gem is not found" , async ( {
80
66
page,
81
67
} ) => {
82
68
// Opens the URL that will be used as the basis for determining the relative URL.
@@ -87,7 +73,8 @@ if (!process.env.RUBY_NPM_PACKAGE_ROOT) {
87
73
<script src="https://cdn.jsdelivr.net/npm/ruby-head-wasm-wasi@latest/dist/browser.script.iife.js">
88
74
</script>
89
75
<script type="text/ruby" data-eval="async">
90
- require_relative 'foo'
76
+ require 'js/require_remote'
77
+ JS::RequireRemote.instance.load 'foo'
91
78
</script>
92
79
` ) ;
93
80
const error = await page . waitForEvent ( "pageerror" ) ;
You can’t perform that action at this time.
0 commit comments