Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/js/moc_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ let () =
method setPublicMetadata entries = set_public_metadata entries
method setRunStepLimit limit = js_set_run_step_limit limit
method setTypecheckerCombineSrcs combineSrcs = Flags.typechecker_combine_srcs := combineSrcs
method setBlobImportPlaceholders placeholders = Flags.blob_import_placeholders := placeholders
method gcFlags option = gc_flags option
method run list s = Flags.compiled := false; wrap_output (fun _ -> js_run list s)
method check s = Flags.compiled := false; js_check s
Expand Down
10 changes: 7 additions & 3 deletions src/lowering/desugar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1215,9 +1215,13 @@ and transform_import (i : S.import) : Ir.dec list =
| S.IDLPath (fp, canister_id) ->
primE (I.ActorOfIdBlob t) [blobE canister_id]
| S.ImportedValuePath path ->
let contents = Lib.FilePath.contents path in
assert T.(t = Prim Blob);
blobE contents
if !Mo_config.Flags.blob_import_placeholders then
raise (Invalid_argument ("blob import placeholder"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, block the desugaring!

else begin
let contents = Lib.FilePath.contents path in
assert T.(t = Prim Blob);
blobE contents
end
in [ letP (pat p) rhs ]

type import_declaration = Ir.dec list
Expand Down
1 change: 1 addition & 0 deletions src/mo_config/flags.ml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ let stable_memory_access_limit = ref stable_memory_access_limit_default
let experimental_stable_memory_default = 0 (* _ < 0: error; _ = 0: warn, _ > 0: allow *)
let experimental_stable_memory = ref experimental_stable_memory_default
let typechecker_combine_srcs = ref false (* useful for the language server *)
let blob_import_placeholders = ref false (* when enabled, blob:file imports resolve as empty blobs *)

let default_warning_levels = M.empty
|> M.add "M0223" Allow (* don't report redundant instantions *)
Expand Down
10 changes: 7 additions & 3 deletions src/mo_interpreter/interpret.ml
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,13 @@ and interpret_exp_mut env exp (k : V.value V.cont) =
| LibPath {path; _} ->
k (find path env.libs)
| ImportedValuePath path ->
let contents = Lib.FilePath.contents path in
assert T.(exp.note.note_typ = Prim Blob);
k (V.Blob contents)
if !Mo_config.Flags.blob_import_placeholders then
trap exp.at "blob import placeholder"
else begin
let contents = Lib.FilePath.contents path in
assert T.(exp.note.note_typ = Prim Blob);
k (V.Blob contents)
end
| IDLPath _ -> trap exp.at "actor import"
| PrimPath -> k (find "@prim" env.libs)
)
Expand Down
25 changes: 16 additions & 9 deletions src/pipeline/resolve_import.ml
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,22 @@ let add_idl_import msgs imported ri_ref at full_path bytes =
err_file_does_not_exist msgs at full_path

let add_value_import msgs imported ri_ref at path =
let add_no_extension _file_exists f = f in
match resolve_lib_import at path add_no_extension with
| Ok full_path -> begin
let ri = ImportedValuePath full_path in
ri_ref := ri;
imported := RIM.add ri at !imported
end
| Error err ->
Diag.add_msg msgs err
if !Mo_config.Flags.blob_import_placeholders then begin
(* When placeholders are enabled, skip file existence check *)
let ri = ImportedValuePath path in
ri_ref := ri;
imported := RIM.add ri at !imported
end else begin
let add_no_extension _file_exists f = f in
match resolve_lib_import at path add_no_extension with
| Ok full_path -> begin
let ri = ImportedValuePath full_path in
ri_ref := ri;
imported := RIM.add ri at !imported
end
| Error err ->
Diag.add_msg msgs err
end

let add_prim_import imported ri_ref at =
ri_ref := PrimPath;
Expand Down
9 changes: 9 additions & 0 deletions test/test-moc.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,12 @@ assert(Motoko.parseMotokoTypedWithScopeCache(/*enable_recovery=*/false, ["bad.mo
// TODO: This requires avoid dropping 'code' field in all checks though all pipeline e.g. infer_prog
// assert(Motoko.parseMotokoTypedWithScopeCache(/*enable_recovery=*/true, ["bad.mo"], new Map()).code != null);

// `blob:` import placeholders
Motoko.setBlobImportPlaceholders(true);
Motoko.saveFile("blob.mo", 'import MyBlob "blob:file:path/to/blob.txt"; MyBlob.size();');
assert(Motoko.parseMotoko(/*enable_recovery=*/true, "blob.mo").code != null);
assert.deepStrictEqual(Motoko.run([], "blob.mo"), {
stdout: "",
stderr: "blob.mo:1.1-1.43: execution error, blob import placeholder\n",
result: { error: {} },
});