|
| 1 | +// An online REPL for gpython using wasm |
| 2 | + |
| 3 | +// Copyright 2018 The go-python Authors. All rights reserved. |
| 4 | +// Use of this source code is governed by a BSD-style |
| 5 | +// license that can be found in the LICENSE file. |
| 6 | + |
| 7 | +// +build js |
| 8 | + |
| 9 | +package main |
| 10 | + |
| 11 | +import ( |
| 12 | + "log" |
| 13 | + "runtime" |
| 14 | + |
| 15 | + "github.com/gopherjs/gopherwasm/js" // gopherjs to wasm converter shim |
| 16 | + |
| 17 | + // import required modules |
| 18 | + _ "github.com/go-python/gpython/builtin" |
| 19 | + _ "github.com/go-python/gpython/math" |
| 20 | + "github.com/go-python/gpython/repl" |
| 21 | + _ "github.com/go-python/gpython/sys" |
| 22 | + _ "github.com/go-python/gpython/time" |
| 23 | +) |
| 24 | + |
| 25 | +// Implement the replUI interface |
| 26 | +type termIO struct { |
| 27 | + js.Value |
| 28 | +} |
| 29 | + |
| 30 | +// SetPrompt sets the UI prompt |
| 31 | +func (t *termIO) SetPrompt(prompt string) { |
| 32 | + t.Call("set_prompt", prompt) |
| 33 | +} |
| 34 | + |
| 35 | +// Print outputs the string to the output |
| 36 | +func (t *termIO) Print(out string) { |
| 37 | + t.Call("echo", out) |
| 38 | +} |
| 39 | + |
| 40 | +var document js.Value |
| 41 | + |
| 42 | +func isUndefined(node js.Value) bool { |
| 43 | + return node == js.Undefined() |
| 44 | +} |
| 45 | + |
| 46 | +func getElementById(name string) js.Value { |
| 47 | + node := document.Call("getElementById", name) |
| 48 | + if isUndefined(node) { |
| 49 | + log.Fatalf("Couldn't find element %q", name) |
| 50 | + } |
| 51 | + return node |
| 52 | +} |
| 53 | + |
| 54 | +func running() string { |
| 55 | + switch { |
| 56 | + case runtime.GOOS == "js" && runtime.GOARCH == "wasm": |
| 57 | + return "go/wasm" |
| 58 | + case runtime.GOARCH == "js": |
| 59 | + return "gopherjs" |
| 60 | + } |
| 61 | + return "unknown" |
| 62 | +} |
| 63 | + |
| 64 | +func main() { |
| 65 | + document = js.Global().Get("document") |
| 66 | + if isUndefined(document) { |
| 67 | + log.Fatalf("Didn't find document - not running in browser") |
| 68 | + } |
| 69 | + |
| 70 | + // Clear the loading text |
| 71 | + termNode := getElementById("term") |
| 72 | + termNode.Set("innerHTML", "") |
| 73 | + |
| 74 | + // work out what we are running on and mark active |
| 75 | + tech := running() |
| 76 | + node := getElementById(tech) |
| 77 | + node.Get("classList").Call("add", "active") |
| 78 | + |
| 79 | + // Make a repl referring to an empty term for the moment |
| 80 | + REPL := repl.New() |
| 81 | + cb := js.NewCallback(func(args []js.Value) { |
| 82 | + REPL.Run(args[0].String()) |
| 83 | + }) |
| 84 | + |
| 85 | + // Create a jquery terminal instance |
| 86 | + opts := js.ValueOf(map[string]interface{}{ |
| 87 | + "greetings": "Gpython 3.4.0 running in your browser with " + tech, |
| 88 | + "name": "gpython", |
| 89 | + "prompt": repl.NormalPrompt, |
| 90 | + }) |
| 91 | + terminal := js.Global().Call("$", "#term").Call("terminal", cb, opts) |
| 92 | + |
| 93 | + // Send the console log direct to the terminal |
| 94 | + js.Global().Get("console").Set("log", terminal.Get("echo")) |
| 95 | + |
| 96 | + // Set the implementation of term |
| 97 | + REPL.SetUI(&termIO{terminal}) |
| 98 | + |
| 99 | + // wait for callbacks |
| 100 | + select {} |
| 101 | +} |
0 commit comments