-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support floats & refactor playground to web assembly
- Loading branch information
Showing
16 changed files
with
725 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
package main | ||
|
||
import "github.com/rabraghib/darijascript/cmd" | ||
import ( | ||
"github.com/rabraghib/darijascript/src/entrypoint" | ||
) | ||
|
||
func main() { | ||
cmd.Execute() | ||
entrypoint.Entrypoint() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
//go:build !js | ||
|
||
package entrypoint | ||
|
||
import "github.com/rabraghib/darijascript/cmd" | ||
|
||
func Entrypoint() { | ||
cmd.Execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//go:build js | ||
|
||
package entrypoint | ||
|
||
import ( | ||
"syscall/js" | ||
|
||
"github.com/rabraghib/darijascript/cmd" | ||
"github.com/rabraghib/darijascript/src/interpreter" | ||
) | ||
|
||
func Entrypoint() { | ||
js.Global().Set("runDarijaScript", js.FuncOf(wasmRunner)) | ||
|
||
select {} | ||
} | ||
|
||
func wasmRunner(this js.Value, p []js.Value) interface{} { | ||
if len(p) != 1 { | ||
return "runDarijaScript() takes exactly 1 argument, " + string(len(p)) + " given" | ||
} | ||
sourceCode := p[0].String() | ||
eval := interpreter.NewEvaluator() | ||
cmd.RunCode(sourceCode, eval) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,3 +34,5 @@ yarn-error.log* | |
# typescript | ||
*.tsbuildinfo | ||
next-env.d.ts | ||
|
||
public/darijascript-bin-web.wasm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import './wasm_exec.js'; | ||
import './wasmTypes.d.ts'; | ||
|
||
import React, { useEffect } from 'react'; | ||
|
||
async function loadWasm(writeOutput: (output: string) => void): Promise<void> { | ||
const goWasm = new window.Go(); | ||
const result = await WebAssembly.instantiateStreaming( | ||
fetch('darijascript-bin-web.wasm'), | ||
goWasm.importObject | ||
); | ||
|
||
let outputBuf = ''; | ||
const decoder = new TextDecoder('utf-8'); | ||
window.fs.writeSync = function (fd, buf) { | ||
const output = decoder.decode(buf); | ||
writeOutput(output); | ||
return buf.length; | ||
}; | ||
goWasm.run(result.instance); | ||
} | ||
|
||
export const LoadWasm: React.FC< | ||
React.PropsWithChildren<{ | ||
writeOutput: (output: string) => void; | ||
}> | ||
> = (props) => { | ||
const [isLoading, setIsLoading] = React.useState(true); | ||
|
||
useEffect(() => { | ||
loadWasm(props.writeOutput).then(() => { | ||
setIsLoading(false); | ||
}); | ||
}, [props.writeOutput]); | ||
|
||
// if (isLoading) { | ||
// return <div>loading WebAssembly...</div>; | ||
// } else { | ||
// } | ||
return <React.Fragment>{props.children}</React.Fragment>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
declare global { | ||
export interface Window { | ||
Go: any; | ||
runDarijaScript: (code: string) => void; | ||
fs: { | ||
writeSync: (fd: number, buf: Uint8Array) => number; | ||
}; | ||
} | ||
} | ||
|
||
export {}; |
Oops, something went wrong.