Skip to content

Commit ea9704d

Browse files
committed
Enabling Byte over ports in the compiler.
Injecting _Json_encodeBytes and _Json_decodeBytes kernel functions directly, not through a customized Json package.
1 parent 922b635 commit ea9704d

File tree

7 files changed

+82
-3
lines changed

7 files changed

+82
-3
lines changed

src/Compiler/Canonicalize/Effects.elm

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ checkPayload tipe =
207207
Can.TType home name args ->
208208
case args of
209209
[] ->
210-
if isJson home name || isString home name || isIntFloatBool home name then
210+
if isJson home name || isString home name || isIntFloatBool home name || isBytes home name then
211211
Ok ()
212212

213213
else
@@ -306,3 +306,11 @@ isArray home name =
306306
== ModuleName.array
307307
&& name
308308
== Name.array
309+
310+
311+
isBytes : IO.Canonical -> Name.Name -> Bool
312+
isBytes home name =
313+
home
314+
== ModuleName.bytes
315+
&& name
316+
== Name.bytes

src/Compiler/Data/Name.elm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Compiler.Data.Name exposing
44
, basics
55
, bitwise
66
, bool
7+
, bytes
78
, char
89
, cmd
910
, debug
@@ -28,6 +29,7 @@ module Compiler.Data.Name exposing
2829
, isKernel
2930
, isNumberType
3031
, jsArray
32+
, json
3133
, list
3234
, mainModule
3335
, main_
@@ -356,6 +358,11 @@ dict =
356358
"Dict"
357359

358360

361+
bytes : Name
362+
bytes =
363+
"Bytes"
364+
365+
359366
tuple : Name
360367
tuple =
361368
"Tuple"
@@ -366,6 +373,11 @@ jsArray =
366373
"JsArray"
367374

368375

376+
json : Name
377+
json =
378+
"Json"
379+
380+
369381
task : Name
370382
task =
371383
"Task"

src/Compiler/Generate/JavaScript.elm

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,30 @@ emptyState startingLine =
267267

268268
stateToBuilder : State -> String
269269
stateToBuilder (State (JS.Builder revKernels code _ _ _) _) =
270-
prependBuilders revKernels code
270+
prependBuilders revKernels (bytesForPorts ++ code)
271+
272+
273+
bytesForPorts : String
274+
bytesForPorts =
275+
"""
276+
// BYTES FOR PORTS
277+
var _Json_decodeBytes = _Json_decodePrim(function(value) {
278+
if (value instanceof Uint8Array) {
279+
return $elm$core$Result$Ok(new DataView(value.buffer, value.byteOffset, value.byteLength));
280+
}
281+
if (value instanceof ArrayBuffer) {
282+
return $elm$core$Result$Ok(new DataView(value));
283+
}
284+
if (value instanceof DataView) {
285+
return $elm$core$Result$Ok(value);
286+
}
287+
return _Json_expecting('a BYTES value (Uint8Array, ArrayBuffer, or DataView)', value);
288+
});
289+
290+
var _Json_encodeBytes = function(bytes) {
291+
return _Json_wrap(new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength));
292+
};
293+
"""
271294

272295

273296
prependBuilders : List String -> String -> String

src/Compiler/Guida/ModuleName.elm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Compiler.Guida.ModuleName exposing
22
( Raw
33
, array
44
, basics
5+
, bytes
56
, canonicalDecoder
67
, canonicalEncoder
78
, char
@@ -283,6 +284,15 @@ jsonEncode =
283284

284285

285286

287+
-- BYTES
288+
289+
290+
bytes : Canonical
291+
bytes =
292+
Canonical Pkg.bytes "Bytes"
293+
294+
295+
286296
-- WEBGL
287297

288298

src/Compiler/Guida/Package.elm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Compiler.Guida.Package exposing
33
, Name
44
, Project
55
, browser
6+
, bytes
67
, compareName
78
, core
89
, decoder
@@ -144,6 +145,11 @@ json =
144145
toName elm "json"
145146

146147

148+
bytes : Name
149+
bytes =
150+
toName elm "bytes"
151+
152+
147153
http : Name
148154
http =
149155
toName elm "http"

src/Compiler/Optimize/Port.elm

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ toEncoder tipe =
5858
else if name == Name.value then
5959
Names.registerGlobal A.zero ModuleName.basics Name.identity_
6060

61+
else if name == Name.bytes then
62+
encodeBytes
63+
6164
else
6265
crash "toEncoder: bad custom type"
6366

@@ -271,6 +274,9 @@ toDecoder tipe =
271274
( "Value", [] ) ->
272275
decode "value"
273276

277+
( "Bytes", [] ) ->
278+
decodeBytes
279+
274280
( "Maybe", [ arg ] ) ->
275281
decodeMaybe arg
276282

@@ -478,3 +484,17 @@ encode name =
478484
decode : Name -> Names.Tracker Opt.Expr
479485
decode name =
480486
Names.registerGlobal A.zero ModuleName.jsonDecode name
487+
488+
489+
490+
-- BYTES HELPERS
491+
492+
493+
encodeBytes : Names.Tracker Opt.Expr
494+
encodeBytes =
495+
Names.registerKernel Name.json (Opt.VarKernel A.zero Name.json "encodeBytes")
496+
497+
498+
decodeBytes : Names.Tracker Opt.Expr
499+
decodeBytes =
500+
Names.registerKernel Name.json (Opt.VarKernel A.zero Name.json "decodeBytes")

src/Compiler/Reporting/Error/Canonicalize.elm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ toReport source err =
704704
[ D.reflow "I cannot handle that. The types that CAN flow in and out of Guida include:"
705705
, D.indent 4 <|
706706
D.reflow
707-
"Ints, Floats, Bools, Strings, Maybes, Lists, Arrays, tuples, records, and JSON values."
707+
"Ints, Floats, Bools, Strings, Maybes, Lists, Arrays, tuples, records, JSON values, and Bytes."
708708
, D.reflow
709709
"Since JSON values can flow through, you can use JSON encoders and decoders to allow other types through as well. More advanced users often just do everything with encoders and decoders for more control and better errors."
710710
]

0 commit comments

Comments
 (0)