Skip to content

Commit f63156e

Browse files
committed
only transpile if needed; use a fresh vm to execute code
1 parent ec5bb2b commit f63156e

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

src/app.js

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Store from "./store.js";
22
import importFromGist from "./gist.js";
3+
import initVM from "./vm.js";
34

45
const DEFAULT_SOURCE = `# Welcome to Ruby Next playground!
56
# Here you can write Ruby code and see how it will be transformed by Ruby Next.
@@ -90,11 +91,12 @@ export default class App {
9091

9192
this.el
9293
.querySelector('[target="run-btn"]')
93-
.addEventListener("click", () => {
94-
const newSource = this.transpile(this.codeEditor.getValue());
95-
this.previewEditor.setValue(newSource);
94+
.addEventListener("click", async () => {
95+
this.invalidatePreview(false);
9696

97-
let { result, output } = this.executeWithOutput(newSource);
97+
const source = this.previewEditor.getValue();
98+
99+
let { result, output } = await this.executeWithOutput(source);
98100

99101
if (result) output += "\n\n> " + result;
100102

@@ -107,6 +109,8 @@ export default class App {
107109
let refreshDebounceId;
108110

109111
this.codeEditor.onDidChangeModelContent((ev) => {
112+
this._dirty = true;
113+
110114
if (!this.autorunCb.checked) return;
111115

112116
if (refreshDebounceId) {
@@ -213,7 +217,7 @@ export default class App {
213217
this.loadExampleFromUrl();
214218
}
215219

216-
refresh() {
220+
async refresh() {
217221
let newSource;
218222
try {
219223
newSource = this.transpile(this.codeEditor.getValue(), { raise: true });
@@ -223,7 +227,7 @@ export default class App {
223227

224228
this.previewEditor.setValue(newSource);
225229

226-
let { result, output } = this.executeWithOutput(newSource);
230+
let { result, output } = await this.executeWithOutput(newSource);
227231

228232
if (result) output += "\n\n> " + result;
229233

@@ -266,27 +270,35 @@ RubyNext.transform(code, **${rubyOptions})
266270
}
267271
}
268272

269-
execute(source) {
273+
async execute(source) {
270274
try {
271-
return this.vm.eval(source).toString();
275+
const vm = await initVM();
276+
return vm.eval(source).toString();
272277
} catch (e) {
273278
console.error(e);
274279
return e.message;
275280
}
276281
}
277282

278-
executeWithOutput(source) {
279-
this.vm.$output.flush();
280-
const result = this.execute(source);
281-
const output = this.vm.$output.flush();
282-
return { result, output };
283+
async executeWithOutput(source) {
284+
try {
285+
const vm = await initVM();
286+
vm.$output.flush();
287+
const result = vm.eval(source).toString();
288+
const output = vm.$output.flush() || "";
289+
console.log(result, output);
290+
return { result, output };
291+
} catch (e) {
292+
console.error(e);
293+
return { output: "💥 " + e.message };
294+
}
283295
}
284296

285297
async setCurrentVMVersion() {
286298
const versionContainer = document.getElementById("currentVersion");
287299
if (!versionContainer) return;
288300

289-
const version = this.execute("RUBY_VERSION + '-' + RUBY_PLATFORM");
301+
const version = await this.execute("RUBY_VERSION + '-' + RUBY_PLATFORM");
290302

291303
versionContainer.innerText = version;
292304
}
@@ -303,12 +315,16 @@ RubyNext.transform(code, **${rubyOptions})
303315
});
304316
}
305317

306-
invalidatePreview() {
318+
invalidatePreview(showPreview = true) {
307319
const version = this.versionSelect.value;
308-
const newSource = this.transpile(this.codeEditor.getValue(), { version });
309-
this.previewEditor.setValue(newSource);
320+
if (this._dirty || this._curVersion != version) {
321+
const newSource = this.transpile(this.codeEditor.getValue(), { version });
322+
this.previewEditor.setValue(newSource);
323+
}
324+
this._dirty = false;
325+
this._curVersion = version;
310326

311-
this.showEditor("previewEditor");
327+
if (showPreview) this.showEditor("previewEditor");
312328
}
313329

314330
showEditor(editorName) {

src/vm.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ import {
88

99
import ruby from "./ruby.wasm";
1010

11+
let rubyModule;
12+
1113
export default async function initVM() {
12-
const module = await ruby();
14+
if (!rubyModule) rubyModule = await ruby();
15+
16+
const module = rubyModule;
1317

1418
const output = [];
1519
output.flush = function () {

0 commit comments

Comments
 (0)