feat: make wasm works

上级 6da30c2a
package main
import (
"fmt"
"encoding/json"
"github.com/antlr/antlr4/runtime/Go/antlr"
"github.com/phodal/coca/core/adapter/coca_file"
"github.com/phodal/coca/core/domain"
"github.com/phodal/coca/core/infrastructure/ast/identifier"
"syscall/js"
)
func main() {
parser := coca_file.ProcessString(`
package com.phodal.coca.analysis.identifier.model;
func registerCallbacks() {
js.Global().Set("compileCode", js.FuncOf(CompileCodeCallback))
}
func main() {
c := make(chan struct{}, 0)
registerCallbacks()
<-c
}
public class DataClass {
private String date;
func CompileCodeCallback(value js.Value, args []js.Value) interface{} {
callback := args[len(args)-1:][0]
message := args[0].String()
public String getDate() {
return date;
}
results := CompileCode(message)
identModel, _ := json.Marshal(results)
callback.Invoke(js.Null(), string(identModel))
return nil
}
`)
func CompileCode(code string) []domain.JIdentifier {
parser := coca_file.ProcessString(code)
context := parser.CompilationUnit()
listener := identifier.NewJavaIdentifierListener()
......@@ -26,5 +39,5 @@ public class DataClass {
antlr.NewParseTreeWalker().Walk(listener, context)
identifiers := listener.GetNodes()
fmt.Println(identifiers)
}
\ No newline at end of file
return identifiers
}
......@@ -6,6 +6,25 @@
const go = new Go();
WebAssembly.instantiateStreaming(fetch("coca.wasm"), go.importObject).then((result) => {
go.run(result.instance);
let code = `
package com.phodal.coca.analysis.identifier.model;
public class DataClass {
private String date;
public String getDate() {
return date;
}
}
`;
compileCode(code, (err, message) => {
if (err) {
console.error(err);
return;
}
console.log(message);
})
});
</script>
</head>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册