Skip to content

Commit

Permalink
Added console.Log
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkauzh committed Oct 2, 2023
1 parent 192ef1d commit ebdabc5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
EXAMPLE_BUILD_TOOL = go run github.com/hajimehoshi/wasmserve@latest

.PHONY: run_example test
.PHONY: example test

run_example:
example:
@echo
@echo " ----------------------------------------------------"
@echo "| Running Example... |"
Expand Down
29 changes: 13 additions & 16 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package main

import (
"flag"
"fmt"
"os"
"syscall/js"
)
import "webzen/src/console"

func main() {
flag.Parse()
fmt.Println(flag.Args())
console.Log("Hello, World!")

p := js.Global().Get("document").Call("createElement", "p")
p.Set("innerText", "Hello, World!")
js.Global().Get("document").Get("body").Call("appendChild", p)

pre := js.Global().Get("document").Call("createElement", "pre")
pre.Set("innerText", fmt.Sprintf("args: %q\nevn: %q", os.Args, os.Environ()))
pre.Get("style").Set("whiteSpace", "pre-wrap")
js.Global().Get("document").Get("body").Call("appendChild", pre)
}

// func main() {
// p := js.Global().Get("document").Call("createElement", "p")
// p.Set("innerText", "Hello, World!")
// js.Global().Get("document").Get("body").Call("appendChild", p)

// pre := js.Global().Get("document").Call("createElement", "pre")
// pre.Set("innerText", fmt.Sprintf("args: %q\nevn: %q", os.Args, os.Environ()))
// pre.Get("style").Set("whiteSpace", "pre-wrap")
// js.Global().Get("document").Get("body").Call("appendChild", pre)
// }
20 changes: 20 additions & 0 deletions src/console/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package console

import (
"fmt"
"syscall/js"
)

func Log(args ...interface{}) {
logArgs := make([]interface{}, len(args))
for i, arg := range args {
// Convert arrays to strings before logging
if arr, ok := arg.([]interface{}); ok {
logArgs[i] = fmt.Sprintf("%v", arr)
} else {
logArgs[i] = arg
}
}

js.Global().Get("console").Call("log", logArgs...)
}
Empty file removed src/main.go
Empty file.

0 comments on commit ebdabc5

Please sign in to comment.