Skip to content

Commit 38ab91a

Browse files
committed
add: replace std/base64 with simdutf
Fixes #37
1 parent ab09b55 commit 38ab91a

File tree

10 files changed

+100
-86
lines changed

10 files changed

+100
-86
lines changed

ferus.nimble

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ requires "stylus >= 0.1.3"
1717
requires "https://github.com/ferus-web/sanchar >= 2.0.2"
1818
requires "https://git.sr.ht/~bptato/chame >= 1.0.1"
1919
requires "seccomp >= 0.2.1"
20-
requires "simdutf >= 5.5.0"
20+
requires "simdutf >= 6.5.1"
2121
requires "https://github.com/ferus-web/bali#master"
2222
requires "results >= 0.5.0"
2323
requires "pretty >= 0.1.0"

src/components/ipc/shared.nim

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import std/[times, net, logging, options, base64]
1+
import std/[times, net, logging, options]
2+
import pkg/simdutf/base64
23

34
proc `*`[T](opt: Option[T]): bool {.inline, noSideEffect, gcsafe.} =
45
# dumb hacks to make code look less yucky
@@ -336,8 +337,8 @@ type
336337
proc `==`*(a, b: FerusProcess): bool {.inline.} =
337338
a.worker == b.worker and a.kind == b.kind and a.socket == b.socket
338339

339-
func content*(packet: DataTransferResult): string {.inline.} =
340-
packet.data.decode()
340+
proc content*(packet: DataTransferResult): string {.inline.} =
341+
packet.data.decode(urlSafe = true)
341342

342343
proc magicFromStr*(s: string): Option[FerusMagic] =
343344
case s

src/components/js/process.nim

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
import std/[logging, json, base64, net]
2-
import ../../components/ipc/client/prelude
3-
import bali/grammar/prelude
4-
import bali/runtime/prelude
5-
import bali/stdlib/console
6-
import jsony
7-
import ../../components/shared/[nix, sugar]
8-
import ../../components/web/[window]
9-
import ../../components/web/document as jsdoc
10-
import ../../components/web/websockets as jswebsocket
1+
import std/[logging, json, net]
2+
import
3+
pkg/jsony,
4+
pkg/simdutf/base64,
5+
pkg/bali/grammar/prelude,
6+
pkg/bali/runtime/prelude,
7+
pkg/bali/stdlib/console
8+
import
9+
../../components/shared/[nix, sugar],
10+
../../components/ipc/client/prelude,
11+
../../components/web/[window],
12+
../../components/web/document as jsdoc,
13+
../../components/web/websockets as jswebsocket,
14+
./ipc
1115
from ../../components/parsers/html/document import HTMLDocument
12-
import ./ipc
1316

1417
type JSProcess* = object
1518
ipc*: IPCClient
@@ -32,8 +35,8 @@ proc jsExecBuffer*(js: var JSProcess, data: string) =
3235
js.ipc.setState(Processing)
3336
let data = &tryParseJson(data, JSExecPacket)
3437

35-
js.parser = newParser(data.buffer.decode())
36-
js.runtime = newRuntime(data.name.decode(), js.parser.parse())
38+
js.parser = newParser(data.buffer.decode(urlSafe = true))
39+
js.runtime = newRuntime(data.name.decode(urlSafe = true), js.parser.parse())
3740
window.generateIR(js.runtime, js.ipc)
3841
jsdoc.generateIR(js.runtime)
3942
jswebsocket.generateBindings(js.runtime, js.ipc)

src/components/master/master.nim

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,20 @@
22
## It essentially allows you to do anything with your own group of processes (renderer, JS runtime, CSS/HTML parsers, etc.)
33
## This includes summoning them, telling them to do a task, telling them to exit, et cetera.
44

5+
import std/[os, logging, osproc, strutils, options, net, sets, terminal, posix, tables]
56
import
6-
std/
7-
[os, logging, osproc, strutils, options, base64, net, sets, terminal, posix, tables]
8-
import ../../components/ipc/server/prelude
9-
import jsony
10-
import ./summon
11-
import pretty
7+
pkg/[jsony, pretty],
8+
pkg/sanchar/parse/url,
9+
pkg/sanchar/proto/http/shared,
10+
pkg/simdutf/base64
1211

13-
# Process specific imports
14-
15-
# Network
16-
import sanchar/parse/url
17-
import sanchar/proto/http/shared
18-
19-
import ../../components/shared/[nix, sugar]
2012
import
13+
../../components/shared/[nix, sugar],
2114
../../components/
22-
[network/ipc, renderer/ipc, parsers/html/ipc, parsers/html/document, js/ipc]
23-
import ../../components/web/cookie/parsed_cookie
15+
[network/ipc, renderer/ipc, parsers/html/ipc, parsers/html/document, js/ipc],
16+
../../components/web/cookie/parsed_cookie,
17+
../../components/ipc/server/prelude,
18+
./summon
2419

2520
when defined(unix):
2621
import std/posix
@@ -139,7 +134,9 @@ proc parseHTML*(
139134

140135
info ("Sending group $1 HTML parser process a request to parse some HTML" % [$group])
141136

142-
master.server.send((&process).socket, ParseHTMLPacket(source: encode(source)))
137+
master.server.send(
138+
(&process).socket, ParseHTMLPacket(source: encode(source, urlSafe = true))
139+
)
143140

144141
info ("Waiting for response from group $1 HTML parser process" % [$group])
145142

@@ -217,7 +214,8 @@ proc executeJS*(
217214
var prc = &process
218215
assert prc.kind == JSRuntime
219216
master.server.send(
220-
prc.socket, JSExecPacket(name: name.encode(), buffer: code.encode())
217+
prc.socket,
218+
JSExecPacket(name: name.encode(urlSafe = true), buffer: code.encode(urlSafe = true)),
221219
)
222220

223221
info "Dispatching JS code to runtime process."
@@ -236,7 +234,7 @@ proc setWindowTitle*(master: MasterProcess, title: string) {.inline.} =
236234
master.server.send(
237235
(&process).socket,
238236
RendererSetWindowTitle(
239-
title: title.encode(safe = true) # So that we can get spaces
237+
title: title.encode(urlSafe = true) # So that we can get spaces
240238
),
241239
)
242240

@@ -288,7 +286,7 @@ proc loadFont*(
288286
let encoded = encode(
289287
# encode the data in base64 to ensure that it doesn't mess up the JSON packet
290288
readFile file,
291-
safe = true,
289+
urlSafe = true,
292290
)
293291
master.server.send(
294292
(&process).socket,

src/components/network/ipc.nim

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import std/[base64, options]
2-
import ../../components/shared/[sugar]
3-
import sanchar/parse/url, sanchar/proto/http/shared, ../../components/ipc/shared
1+
#!fmt: off
2+
import std/[options]
3+
import pkg/sanchar/parse/url,
4+
pkg/sanchar/proto/http/shared,
5+
pkg/simdutf/base64
6+
import ../../components/ipc/shared,
7+
../../components/shared/[sugar]
8+
#!fmt: on
49

510
type
611
NetworkFetchPacket* = object
@@ -20,8 +25,8 @@ type
2025
kind*: FerusMagic = feNetworkWebSocketCreationResult
2126
error*: Option[string]
2227

23-
func content*(res: NetworkFetchResult): string {.inline.} =
28+
proc content*(res: NetworkFetchResult): string {.inline.} =
2429
if !res.response:
2530
return
2631

27-
(&res.response).content.decode()
32+
(&res.response).content.decode(urlSafe = true)

src/components/network/process.nim

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
import std/[base64, strutils, sequtils, importutils, logging, options, os, json, net]
2-
import pkg/sanchar/[http, proto/http/shared], pkg/sanchar/parse/url
3-
import pkg/pretty
4-
import pkg/whisky
1+
import std/[strutils, sequtils, importutils, logging, options, os, json, net]
2+
import
3+
pkg/[pretty, whisky, jsony],
4+
pkg/sanchar/[http, proto/http/shared],
5+
pkg/sanchar/parse/url,
6+
pkg/simdutf/base64
57

68
when defined(ferusUseCurl):
7-
import pkg/webby/httpheaders
8-
import pkg/curly
9+
#!fmt:off
10+
import pkg/webby/httpheaders, pkg/curly #!fmt:on
911

10-
import pkg/jsony
11-
import ../../components/shared/[nix, sugar]
12-
import ../../components/network/[websocket, types, ipc]
13-
import ../../components/build_utils
14-
import ../../components/ipc/client/prelude
12+
import
13+
../../components/shared/[nix, sugar],
14+
../../components/network/[websocket, types, ipc],
15+
../../components/build_utils,
16+
../../components/ipc/client/prelude
1517

1618
privateAccess(WebSocket)
1719

@@ -125,7 +127,7 @@ proc talk(client: FerusNetworkClient, process: FerusProcess) {.inline.} =
125127
return
126128

127129
var resp = &odata.response
128-
resp.content = resp.content.encode()
130+
resp.content = resp.content.encode(urlSafe = true)
129131

130132
odata.response = some(move(resp))
131133

src/components/parsers/html/document.nim

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
## I love chame
22

3-
import std/[options, logging, strutils, tables, base64]
4-
import chagashi/charset
5-
import sanchar/parse/url
6-
import ../../shared/sugar
7-
import ../../web/dom
8-
import pretty
3+
#!fmt:off
4+
import std/[options, logging, strutils, tables]
5+
import pkg/[pretty], pkg/chagashi/charset, pkg/sanchar/parse/url, pkg/simdutf/base64
6+
import ../../shared/sugar, ../../web/dom
7+
#!fmt:on
98

109
type
1110
HTMLElement* = ref object
@@ -55,15 +54,15 @@ func findAll*(
5554

5655
res
5756

58-
func text*(elem: HTMLElement): Option[string] {.inline.} =
57+
proc text*(elem: HTMLElement): Option[string] {.inline.} =
5958
if *elem.text:
60-
return some(decode(&elem.text))
59+
return some(decode(&elem.text, urlSafe = true))
6160

62-
func attribute*(elem: HTMLElement, name: string): Option[string] {.inline.} =
61+
proc attribute*(elem: HTMLElement, name: string): Option[string] {.inline.} =
6362
if name in elem.attributes:
64-
return some(decode(elem.attributes[name]))
63+
return some(decode(elem.attributes[name], urlSafe = true))
6564

66-
func classes*(elem: HTMLElement): seq[string] {.inline.} =
65+
proc classes*(elem: HTMLElement): seq[string] {.inline.} =
6766
let classAttr = elem.attribute("class")
6867
if !classAttr:
6968
return
@@ -79,7 +78,7 @@ proc parseHTMLElement*(document: Document, element: Element): HTMLElement =
7978
for (prefix, namespace, name, value) in element.attrs:
8079
# we currently don't really care about the namespace
8180
let mappedName = document.factory.atomToStr(name)
82-
elem.attributes[mappedName] = encode(value)
81+
elem.attributes[mappedName] = encode(value, urlSafe = true)
8382

8483
case tag
8584
of TAG_P,
@@ -97,7 +96,7 @@ proc parseHTMLElement*(document: Document, element: Element): HTMLElement =
9796
for txt in element.textNodes:
9897
text &= txt.data
9998

100-
elem.text = some(encode(text))
99+
elem.text = some(encode(text, urlSafe = true))
101100
else:
102101
discard
103102

src/components/parsers/html/process.nim

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import std/[base64, options, json, logging, monotimes, net, os]
2-
import ./ipc
3-
import ./document
4-
import ../../web/dom
5-
import ../../shared/[nix, sugar]
6-
import ../../../components/ipc/client/prelude
7-
import jsony
1+
import std/[options, json, logging, monotimes, net, os]
2+
import pkg/jsony, pkg/simdutf/base64
3+
import
4+
./[document, ipc],
5+
../../web/dom,
6+
../../shared/[nix, sugar],
7+
../../../components/ipc/client/prelude
88

99
proc htmlParse*(oparsingData: Option[ParseHTMLPacket]): HTMLParseResult =
1010
if !oparsingData:
@@ -17,7 +17,7 @@ proc htmlParse*(oparsingData: Option[ParseHTMLPacket]): HTMLParseResult =
1717

1818
let
1919
startTime = getMonoTime()
20-
document = parseHTML(newStringStream(decode(parsingData.source)))
20+
document = parseHTML(newStringStream(decode(parsingData.source, urlSafe = true)))
2121
endTime = getMonoTime()
2222

2323
info "Parsed HTML in " & $(endTime - startTime)

src/components/renderer/ipc.nim

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import std/base64
21
import ../../components/ipc/shared
32
import pkg/vmath
43
from pkg/ferusgfx import Scene
5-
import pkg/ferusgfx/displaylist
6-
4+
import pkg/ferusgfx/displaylist, pkg/simdutf/base64
75
import ../parsers/html/document
86

97
type
@@ -58,17 +56,24 @@ proc parseHook*(s: string, i: int, v2: ptr Scene) {.inline.} =
5856

5957
proc newTextNode*(content: string, position: Vec2, font: string): Drawable {.inline.} =
6058
Drawable(
61-
kind: TextNode, content: content.encode(safe = true), position: position, font: font
59+
kind: TextNode,
60+
content: content.encode(urlSafe = true),
61+
position: position,
62+
font: font,
6263
)
6364

6465
proc newImageNode*(path: string, position: Vec2): Drawable {.inline.} =
6566
Drawable(
66-
kind: ImageNode, imgContent: path.readFile().encode(safe = true), position: position
67+
kind: ImageNode,
68+
imgContent: path.readFile().encode(urlSafe = true),
69+
position: position,
6770
)
6871

6972
proc newGIFNode*(path: string, position: Vec2): Drawable {.inline.} =
7073
Drawable(
71-
kind: GIFNode, gifContent: path.readFile().encode(safe = true), position: position
74+
kind: GIFNode,
75+
gifContent: path.readFile().encode(urlSafe = true),
76+
position: position,
7277
)
7378

7479
proc newDisplayList*(clearAll: bool = false): IPCDisplayList {.inline.} =

src/components/renderer/process.nim

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import std/[options, json, logging, base64, importutils, logging, posix, net]
2-
import jsony
3-
import ferusgfx/[displaylist, fontmgr, textnode, imagenode, gifnode]
4-
import pixie
1+
import std/[options, json, logging, importutils, logging, posix, net]
2+
import
3+
pkg/ferusgfx/[displaylist, fontmgr, textnode, imagenode, gifnode],
4+
pkg/[jsony, pixie],
5+
pkg/simdutf/base64
56
when defined(linux):
67
import ../../components/sandbox/linux
78

@@ -45,7 +46,7 @@ proc loadFont*(
4546
font: Option[Font]
4647

4748
try:
48-
typeface = some decode(data).readTypeface(packet.format)
49+
typeface = some cast[string](decodeSeq(data, urlSafe = true)).parseTtf()
4950
except PixieError as exc:
5051
error "Failed to load typeface: " & exc.msg & ": fmt=" & packet.format
5152
return
@@ -135,7 +136,7 @@ proc talk(
135136
return
136137

137138
client.setState(Processing)
138-
renderer.setWindowTitle((&reinterpreted).title.decode())
139+
renderer.setWindowTitle((&reinterpreted).title.decode(urlSafe = true))
139140
client.setState(Idling)
140141
of feRendererRenderDocument:
141142
let reinterpreted = tryParseJson(data, RendererRenderDocument)

0 commit comments

Comments
 (0)