-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathmain.swift
59 lines (51 loc) · 1.92 KB
/
main.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import JavaScriptKit
let alert = JSObject.global.alert.function!
let document = JSObject.global.document
print("Hello from WASM, document title: \(document.title.string ?? "")")
var count = 0
var divElement = document.createElement("div")
divElement.innerText = .string("Count \(count)")
_ = document.body.appendChild(divElement)
var clickMeElement = document.createElement("button")
clickMeElement.innerText = "Click me"
clickMeElement.onclick = JSValue.object(
JSClosure { _ in
count += 1
divElement.innerText = .string("Count \(count)")
return .undefined
}
)
_ = document.body.appendChild(clickMeElement)
var encodeResultElement = document.createElement("pre")
var textInputElement = document.createElement("input")
textInputElement.type = "text"
textInputElement.placeholder = "Enter text to encode to UTF-8"
textInputElement.oninput = JSValue.object(
JSClosure { _ in
let textEncoder = JSObject.global.TextEncoder.function!.new()
let encode = textEncoder.encode.function!
let encodedData = JSTypedArray<UInt8>(
unsafelyWrapping: encode(this: textEncoder, textInputElement.value).object!
)
encodeResultElement.innerText = .string(
encodedData.withUnsafeBytes { bytes in
bytes.map { hex($0) }.joined(separator: " ")
}
)
return .undefined
}
)
let encoderContainer = document.createElement("div")
_ = encoderContainer.appendChild(textInputElement)
_ = encoderContainer.appendChild(encodeResultElement)
_ = document.body.appendChild(encoderContainer)
func print(_ message: String) {
_ = JSObject.global.console.log(message)
}
func hex(_ value: UInt8) -> String {
var result = "0x"
let hexChars: [Character] = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"]
result.append(hexChars[Int(value / 16)])
result.append(hexChars[Int(value % 16)])
return result
}