Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Compiler/Canonicalize/Effects.elm
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ checkPayload tipe =
Can.TType home name args ->
case args of
[] ->
if isJson home name || isString home name || isIntFloatBool home name then
if isJson home name || isString home name || isIntFloatBool home name || isBytes home name then
Ok ()

else
Expand Down Expand Up @@ -306,3 +306,11 @@ isArray home name =
== ModuleName.array
&& name
== Name.array


isBytes : IO.Canonical -> Name.Name -> Bool
isBytes home name =
home
== ModuleName.bytes
&& name
== Name.bytes
12 changes: 12 additions & 0 deletions src/Compiler/Data/Name.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Compiler.Data.Name exposing
, basics
, bitwise
, bool
, bytes
, char
, cmd
, debug
Expand All @@ -28,6 +29,7 @@ module Compiler.Data.Name exposing
, isKernel
, isNumberType
, jsArray
, json
, list
, mainModule
, main_
Expand Down Expand Up @@ -356,6 +358,11 @@ dict =
"Dict"


bytes : Name
bytes =
"Bytes"


tuple : Name
tuple =
"Tuple"
Expand All @@ -366,6 +373,11 @@ jsArray =
"JsArray"


json : Name
json =
"Json"


task : Name
task =
"Task"
Expand Down
25 changes: 24 additions & 1 deletion src/Compiler/Generate/JavaScript.elm
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,30 @@ emptyState startingLine =

stateToBuilder : State -> String
stateToBuilder (State (JS.Builder revKernels code _ _ _) _) =
prependBuilders revKernels code
prependBuilders revKernels (bytesForPorts ++ code)


bytesForPorts : String
bytesForPorts =
"""
// BYTES FOR PORTS
var _Json_decodeBytes = _Json_decodePrim(function(value) {
if (value instanceof Uint8Array) {
return $elm$core$Result$Ok(new DataView(value.buffer, value.byteOffset, value.byteLength));
}
if (value instanceof ArrayBuffer) {
return $elm$core$Result$Ok(new DataView(value));
}
if (value instanceof DataView) {
return $elm$core$Result$Ok(value);
}
return _Json_expecting('a BYTES value (Uint8Array, ArrayBuffer, or DataView)', value);
});

var _Json_encodeBytes = function(bytes) {
return _Json_wrap(new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength));
};
"""


prependBuilders : List String -> String -> String
Expand Down
10 changes: 10 additions & 0 deletions src/Compiler/Guida/ModuleName.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Compiler.Guida.ModuleName exposing
( Raw
, array
, basics
, bytes
, canonicalDecoder
, canonicalEncoder
, char
Expand Down Expand Up @@ -283,6 +284,15 @@ jsonEncode =



-- BYTES


bytes : Canonical
bytes =
Canonical Pkg.bytes "Bytes"



-- WEBGL


Expand Down
6 changes: 6 additions & 0 deletions src/Compiler/Guida/Package.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Compiler.Guida.Package exposing
, Name
, Project
, browser
, bytes
, compareName
, core
, decoder
Expand Down Expand Up @@ -144,6 +145,11 @@ json =
toName elm "json"


bytes : Name
bytes =
toName elm "bytes"


http : Name
http =
toName elm "http"
Expand Down
20 changes: 20 additions & 0 deletions src/Compiler/Optimize/Port.elm
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ toEncoder tipe =
else if name == Name.value then
Names.registerGlobal A.zero ModuleName.basics Name.identity_

else if name == Name.bytes then
encodeBytes

else
crash "toEncoder: bad custom type"

Expand Down Expand Up @@ -271,6 +274,9 @@ toDecoder tipe =
( "Value", [] ) ->
decode "value"

( "Bytes", [] ) ->
decodeBytes

( "Maybe", [ arg ] ) ->
decodeMaybe arg

Expand Down Expand Up @@ -478,3 +484,17 @@ encode name =
decode : Name -> Names.Tracker Opt.Expr
decode name =
Names.registerGlobal A.zero ModuleName.jsonDecode name



-- BYTES HELPERS


encodeBytes : Names.Tracker Opt.Expr
encodeBytes =
Names.registerKernel Name.json (Opt.VarKernel A.zero Name.json "encodeBytes")


decodeBytes : Names.Tracker Opt.Expr
decodeBytes =
Names.registerKernel Name.json (Opt.VarKernel A.zero Name.json "decodeBytes")
2 changes: 1 addition & 1 deletion src/Compiler/Reporting/Error/Canonicalize.elm
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ toReport source err =
[ D.reflow "I cannot handle that. The types that CAN flow in and out of Guida include:"
, D.indent 4 <|
D.reflow
"Ints, Floats, Bools, Strings, Maybes, Lists, Arrays, tuples, records, and JSON values."
"Ints, Floats, Bools, Strings, Maybes, Lists, Arrays, tuples, records, JSON values, and Bytes."
, D.reflow
"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."
]
Expand Down
Loading