Skip to content

Commit 49cc3e8

Browse files
Add "env" option
1 parent 2d09a8d commit 49cc3e8

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/index.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,14 @@ function rubySourceFromURL(): RubySource | null {
310310
return { type: "builtin", version: "3.4" }
311311
}
312312

313-
type Options = {
313+
export type Options = {
314314
arguments: string[],
315+
env: Record<string, string>,
316+
}
317+
318+
const DEFAULT_OPTIONS: Options = {
319+
arguments: [],
320+
env: {},
315321
}
316322

317323
type UIState = {
@@ -483,9 +489,7 @@ puts RUBY_DESCRIPTION`
483489

484490
let options = JSON.parse(query.get("options")) as Options | null
485491
if (options == null) {
486-
options = {
487-
arguments: [],
488-
}
492+
options = DEFAULT_OPTIONS
489493
}
490494

491495
return { code, action, options }
@@ -660,12 +664,12 @@ export async function init(config: PlayRubyConfig) {
660664
const runCode = async (code: string) => {
661665
const selectedAction = actionSelect.value
662666
outputPane.innerText = ""
663-
let args: string[] = []
667+
let options: Options = DEFAULT_OPTIONS
664668
const outputWriter = (selectedAction == "compile" || selectedAction == "syntax" || selectedAction == "syntax+prism")
665669
? new LocationHighlightingOutputWriter(outputPane, editor)
666670
: new PlainOutputWriter(outputPane)
667671
try {
668-
args = getOptions().arguments
672+
options = getOptions()
669673
} catch (error) {
670674
outputWriter.write(`Error parsing options: ${error.message}\n`)
671675
return;
@@ -677,7 +681,7 @@ export async function init(config: PlayRubyConfig) {
677681
// Prepend empty lines to the file content to match the original source line
678682
codeMap[filename] = "\n".repeat(file.sourceLine + 1) + file.content
679683
}
680-
await worker.run(codeMap, mainFile, selectedAction, args, Comlink.proxy((text) => outputWriter.write(text)))
684+
await worker.run(codeMap, mainFile, selectedAction, options, Comlink.proxy((text) => outputWriter.write(text)))
681685
outputWriter.finalize()
682686
}
683687
const run = async () => await runCode(getCode());

src/ruby.worker.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Directory, File, Inode, OpenFile, PreopenDirectory, WASI, wasi } from "@bjorn3/browser_wasi_shim"
22
import * as Comlink from "comlink"
33
import { IFs, RubyInstall } from "./ruby-install"
4+
import type { Options } from "./index"
45

56

67
type IDir = Pick<Directory, "get_entry_for_path" | "create_entry_for_path" | "contents">;
@@ -232,7 +233,8 @@ export class RubyWorker {
232233
return "3.3.0"
233234
}
234235

235-
async run(code: { [path: string]: string }, mainScriptPath: string, action: string, extraArgs: string[], log: (message: string) => void) {
236+
async run(code: { [path: string]: string }, mainScriptPath: string, action: string, options: Options, log: (message: string) => void) {
237+
const extraArgs: string[] = options.arguments
236238
switch (action) {
237239
case "eval": break
238240
case "compile": extraArgs.push("--dump=insns"); break
@@ -267,7 +269,7 @@ export class RubyWorker {
267269
// Run the Ruby module with the given code
268270
const wasi = new WASI(
269271
["ruby"].concat(extraArgs).concat([mainScriptPath]),
270-
[],
272+
Object.entries(options.env).map(([key, value]) => `${key}=${value}`),
271273
[
272274
new OpenFile(new File([])), // stdin
273275
new OpenFile(new File([])), // stdout

0 commit comments

Comments
 (0)