Host bundles in-process in zx and give them files, clock, arguments and assets
zx now opens the bundle with the linked runtime (std/bundle_host), binds every capability it imports from its own table and runs it natively. A capability it lacks is named in the error; nothing else runs the app. The base capabilities are declared in std/guest_assets (the bundle's own embedded files), std/guest_clock, std/guest_files and std/guest_process (arguments and environment), and answered by cmd/zx_runner_capabilities.zi. Before this a bundle could only print. The install_user check runs a bundle that copies an embedded asset to a file named by its argument and reads it back.
This commit is contained in:
1 parent
777116fd67
commit
d6cf04323c
18 files changed
+588
-98
No files matched your search
@@ -348,9 +348,12 @@ HOST_EXEC_SOURCES := cmd/zir/zir_host_exec.c cmd/zir/zir_host_native.c cmd/zir/z
|
||||
ENGINE_NAMES := $(ENGINE_C)/engine_names.h
|
||||
ifeq ($(BOOTSTRAP),1)
|
||||
ENGINE_OBJECTS :=
|
||||
ENGINE_FACADE_OBJECT :=
|
||||
$(call obj,$(HOST_EXEC_SOURCES)): CFLAGS += -DZIRAN_NO_ENGINE
|
||||
else
|
||||
ENGINE_OBJECTS := $(patsubst %,$(BUILD_DIR)/obj/engine/%.o,$(ENGINE_MODULES))
|
||||
# The facade over the engine (cmd/compiler_zx_exec*.zi), generated with it.
|
||||
ENGINE_FACADE_OBJECT := $(BUILD_DIR)/obj/compiler-zx-exec.o
|
||||
# Per-object flags, not a target-specific CFLAGS: that would also reach the
|
||||
# prerequisites make builds for these objects (zi2c, through zir_host_native's
|
||||
# generated assembly headers), and engine_names.h does not exist yet there.
|
||||
@@ -359,7 +362,7 @@ $(call obj,$(HOST_EXEC_SOURCES)): $(ENGINE_NAMES)
|
||||
$(call obj,cmd/zx2wasm/main.c)_CFLAGS += -iquote $(ENGINE_C) -include $(ENGINE_NAMES)
|
||||
$(call obj,cmd/zx2wasm/main.c): $(ENGINE_NAMES)
|
||||
endif
|
||||
LIB_OBJECTS := $(call obj,$(LIB_SOURCES) $(HOST_EXEC_SOURCES)) $(ENGINE_OBJECTS) $(call compiler-port-object,host_api) $(BUNDLE_MODULE_OBJECT) $(SCANNER_OBJECT) $(TEXT_OBJECT) $(SOURCE_OBJECT) $(DECLARATION_OBJECT) $(ENUM_OBJECT) $(TYPE_OBJECT) $(EXPRESSION_OBJECT) $(STATEMENT_OBJECT) $(PROOF_KERNEL_OBJECT) $(PROOF_OBJECT) $(IR_OBJECT) $(LAW_OBJECT) $(SERVICES_OBJECT) $(LINK_OBJECT) $(CHECK_PORT_OBJECTS) \
|
||||
LIB_OBJECTS := $(call obj,$(LIB_SOURCES) $(HOST_EXEC_SOURCES)) $(ENGINE_OBJECTS) $(ENGINE_FACADE_OBJECT) $(call compiler-port-object,host_api) $(BUNDLE_MODULE_OBJECT) $(SCANNER_OBJECT) $(TEXT_OBJECT) $(SOURCE_OBJECT) $(DECLARATION_OBJECT) $(ENUM_OBJECT) $(TYPE_OBJECT) $(EXPRESSION_OBJECT) $(STATEMENT_OBJECT) $(PROOF_KERNEL_OBJECT) $(PROOF_OBJECT) $(IR_OBJECT) $(LAW_OBJECT) $(SERVICES_OBJECT) $(LINK_OBJECT) $(CHECK_PORT_OBJECTS) \
|
||||
$(BUILD_DIR)/obj/parse.o $(RANGE_OBJECTS) $(NATIVE_NAMES_OBJECT) \
|
||||
$(call obj,cmd/zir/zir_zx_wasm_web.c)
|
||||
NATIVE_HOST_ENABLED := 1
|
||||
@@ -687,16 +690,17 @@ $(BIN_DIR)/zi-fmt: $(FORMAT_SOURCES) $(BIN_DIR)/zi2c $(BUILD_DIR)/.compiler-flag
|
||||
-o $(FORMAT_C) cmd/format.zi
|
||||
$(NICE) $(CC) $(CFLAGS) -I$(FORMAT_C) -o $@ $(FORMAT_C)/*.c -lm
|
||||
|
||||
# ZX Runner, the `zx` command: runs any .zx program with this toolchain.
|
||||
# ZX Runner, the `zx` command: hosts any .zx program in-process with the
|
||||
# runtime and answers its capabilities (cmd/zx_runner_capabilities.zi).
|
||||
ZX_RUNNER_C := $(BUILD_DIR)/zx-runner-c
|
||||
ZX_RUNNER_SOURCES := cmd/zx_runner.zi cmd/zx_runner_header.zi cmd/zx_runner_dialog.zi cmd/compiler_bundle.zi \
|
||||
std/byte_text_linux.zi std/c_string.zi std/environment_linux.zi std/file.zi std/file_linux.zi \
|
||||
std/process_run_linux.zi std/stdio_linux.zi std/text_tools.zi std/vec.zi
|
||||
$(BIN_DIR)/zx: $(ZX_RUNNER_SOURCES) $(BIN_DIR)/zi2c $(BUILD_DIR)/.compiler-flags | $(BIN_DIR)
|
||||
ZX_RUNNER_SOURCES := $(wildcard cmd/zx_runner*.zi) std/bundle_host.zi std/byte_text_linux.zi \
|
||||
std/c_string.zi std/environment_linux.zi std/file.zi std/file_linux.zi std/file_tree_linux.zi \
|
||||
std/process_run_linux.zi std/stdio_linux.zi std/text_tools.zi std/timer_linux.zi std/vec.zi
|
||||
$(BIN_DIR)/zx: $(ZX_RUNNER_SOURCES) $(BIN_DIR)/zi2c $(BUILD_DIR)/libziran.a $(BUILD_DIR)/.compiler-flags | $(BIN_DIR)
|
||||
rm -rf $(ZX_RUNNER_C)
|
||||
$(BIN_DIR)/zi2c --entry zx_runner:main $(ZIRAN_DEFINES) --root cmd --module-path std \
|
||||
-o $(ZX_RUNNER_C) cmd/zx_runner.zi
|
||||
$(NICE) $(CC) $(CFLAGS) -I$(ZX_RUNNER_C) -o $@ $(ZX_RUNNER_C)/*.c -lm
|
||||
$(NICE) $(CC) $(CFLAGS) -I$(ZX_RUNNER_C) -o $@ $(ZX_RUNNER_C)/*.c $(BUILD_DIR)/libziran.a $(RUNTIME_LIBS)
|
||||
|
||||
EDIT_C := $(BUILD_DIR)/edit-c
|
||||
$(BIN_DIR)/zi-edit: cmd/edit.zi cmd/transport.zi cmd/format_source.zi cmd/diagnostics.zi std/json_document.zi cmd/zir/zir_edit.c cmd/zir/zir_edit.h $(BIN_DIR)/zi2c $(FRONTEND_OBJECTS) $(BUNDLE_MODULE_OBJECT) | $(BIN_DIR)
|
||||
@@ -1057,10 +1061,10 @@ $(PROOF_C)/.generated: cmd/compiler_proof.zi cmd/compiler_ir.zi cmd/compiler_ser
|
||||
|
||||
$(ENGINE_C)/.generated: cmd/compiler_runtime.zi cmd/compiler_exec_link.zi cmd/compiler_exec_debug.zi cmd/compiler_exec.zi cmd/compiler_opt.zi cmd/compiler_opt_ssa.zi cmd/compiler_opt_rotate.zi cmd/compiler_opt_loop.zi cmd/compiler_opt_licm.zi cmd/compiler_opt_induction.zi cmd/compiler_opt_layout.zi cmd/compiler_opt_inline.zi cmd/compiler_opt_specialize.zi cmd/compiler_opt_thread.zi cmd/compiler_opt_accumulate.zi cmd/compiler_opt_dom.zi cmd/compiler_learning.zi cmd/compiler_learning_policy.zi cmd/compiler_learning_table.zi \
|
||||
cmd/compiler_exec_serial.zi cmd/compiler_lowered_verify.zi cmd/compiler_lowered_proven.zi cmd/compiler_lowered_ir.zi \
|
||||
cmd/compiler_bundle.zi std/math.zi std/finite.zi std/text_buffer.zi std/number_text.zi $(wildcard cmd/compiler_zx_wasm*.zi) $(BOOTSTRAP_BIN)
|
||||
cmd/compiler_bundle.zi std/math.zi std/finite.zi std/text_buffer.zi std/number_text.zi $(wildcard cmd/compiler_zx_wasm*.zi) $(wildcard cmd/compiler_zx_exec*.zi) $(BOOTSTRAP_BIN)
|
||||
env -u DISPLAY -u WAYLAND_DISPLAY $(BOOTSTRAP_BIN) --no-main --root cmd \
|
||||
--define ZIRAN_EXEC_NATIVE_PLAN_INDEXES --define ZIRAN_EXEC_PACKED_INSTRUCTIONS \
|
||||
-o $(ENGINE_C) cmd/compiler_runtime.zi
|
||||
-o $(ENGINE_C) cmd/compiler_runtime.zi cmd/compiler_zx_exec.zi
|
||||
touch $@
|
||||
|
||||
$(ENGINE_NAMES): $(ENGINE_C)/.generated scripts/engine-names.sh
|
||||
@@ -1071,6 +1075,13 @@ $(BUILD_DIR)/obj/engine/%.o: Makefile $(ENGINE_NAMES) $(BUILD_DIR)/.compiler-fla
|
||||
$(NICE) $(CC) $(CFLAGS) $(FRAMEFLAGS) -fno-math-errno -fvisibility=hidden -iquote $(ENGINE_C) -include $(ENGINE_NAMES) \
|
||||
-DZIRAN_ENGINE_SOURCE_$* -c -o $@ $(ENGINE_C)/$*.c
|
||||
|
||||
# The facade reaches the engine under its private names; its own C entry
|
||||
# points, outside ENGINE_MODULES, keep theirs.
|
||||
$(BUILD_DIR)/obj/compiler-zx-exec.o: $(ENGINE_NAMES) $(BUILD_DIR)/.compiler-flags
|
||||
@mkdir -p $(dir $@)
|
||||
$(NICE) $(CC) $(CFLAGS) $(FRAMEFLAGS) -iquote $(ENGINE_C) -include $(ENGINE_NAMES) \
|
||||
-c -o $@ $(ENGINE_C)/compiler_zx_exec.c
|
||||
|
||||
|
||||
$(BUNDLE_C)/.generated: cmd/compiler_bundle.zi $(BOOTSTRAP_BIN)
|
||||
env -u DISPLAY -u WAYLAND_DISPLAY $(BOOTSTRAP_BIN) --no-main --root cmd \
|
||||
|
||||
+123
-65
@@ -1,44 +1,38 @@
|
||||
// zx, the ZX Runner: runs any portable .zx program, as Linux runs an ELF
|
||||
// file, with the runtime of the toolchain installed beside it.
|
||||
// `zx FILE.zx [ARGUMENT...]`.
|
||||
// file. `zx FILE.zx [ARGUMENT...]`.
|
||||
//
|
||||
// The runner knows no library: a bundle carries all of its own code and
|
||||
// imports only the runner's host capabilities. A capability this runner
|
||||
// lacks is an error, never a hand-off to another program. The header is
|
||||
// checked first, so a wrong file, or one made by an older or newer Ziran,
|
||||
// gets a plain explanation. Started without a terminal, as from a file
|
||||
// manager, the runner shows any failure in a dialog as well.
|
||||
// imports only host capabilities, which the runner answers itself
|
||||
// (zx_runner_capabilities). A capability it lacks is an error naming it,
|
||||
// never a hand-off to another program. The header is checked first, so a
|
||||
// wrong file, or one made by an older or newer Ziran, gets a plain
|
||||
// explanation. Started without a terminal, as from a file manager, the
|
||||
// runner shows any failure in a dialog as well.
|
||||
#import "std/bundle_host"
|
||||
#import "std/byte_text_linux"
|
||||
#import "std/c_string"
|
||||
#import "std/environment_linux"
|
||||
#import "std/file"
|
||||
#import "std/file_linux"
|
||||
#import "std/process_run_linux"
|
||||
#import "std/stdio_linux"
|
||||
#import "std/text_tools"
|
||||
#import "zx_runner_capabilities"
|
||||
#import "zx_runner_dialog"
|
||||
#import "zx_runner_header"
|
||||
using Vectors :: #import "std/vec"
|
||||
|
||||
#scope_file
|
||||
// The engine's status for a bundle that needs a capability it lacks.
|
||||
GAP :: 3
|
||||
libc :: #system_library "libc";
|
||||
TerminalRaw :: (fd: s32) -> s32 #foreign libc "isatty";
|
||||
ReadLinkRaw :: (path: *u8, output: *u8, capacity: usize) -> isize #foreign libc "readlink";
|
||||
ProcessIdRaw :: () -> s32 #foreign libc "getpid";
|
||||
|
||||
// The directory holding this executable, where its toolchain lives.
|
||||
ToolDirectory :: (storage: []u8) -> string {
|
||||
link: [32]u8
|
||||
if !CopyCString("/proc/self/exe", link[:]) { return "" }
|
||||
length := cast(s64)ReadLinkRaw(*link[0], *storage[0], cast(usize)(storage.count - 1))
|
||||
if length <= 0 || length >= storage.count - 1 { return "" }
|
||||
slash := length - 1
|
||||
while slash > 0 && storage[slash] != #char "/" { slash -= 1 }
|
||||
if slash <= 0 { return "" }
|
||||
return TextFromBytes(storage[0:slash])
|
||||
}
|
||||
DuplicateRaw :: (fd: s32) -> s32 #foreign libc "dup";
|
||||
ReplaceRaw :: (source: s32, destination: s32) -> s32 #foreign libc "dup2";
|
||||
OpenRaw :: (path: *u8, flags: s32, mode: ..any) -> s32 #foreign libc "open";
|
||||
CloseRaw :: (fd: s32) -> s32 #foreign libc "close";
|
||||
WRITE_TRUNCATE_CREATE :: 577;
|
||||
STANDARD_ERROR :: 2;
|
||||
// More missing capabilities than this are counted, not listed.
|
||||
LISTED_GAPS :: 6;
|
||||
|
||||
// Says what went wrong on standard error and, when nobody is watching a
|
||||
// terminal, in a dialog.
|
||||
@@ -51,7 +45,7 @@ Fail :: (message: string, attended: bool, code: s32) -> s32 {
|
||||
return code
|
||||
}
|
||||
|
||||
// A private file for the program's standard error while unattended.
|
||||
// A private file that holds the engine's standard error while unattended.
|
||||
ErrorLogPath :: () -> Text {
|
||||
directory := EnvironmentText("XDG_RUNTIME_DIR")
|
||||
if directory == "" || directory[0] != #char "/" { directory = "/tmp" }
|
||||
@@ -69,10 +63,66 @@ Tail :: (text: string, limit: s64) -> string {
|
||||
return text[start + 1:]
|
||||
}
|
||||
|
||||
// Binds every capability the bundle imports and returns how many this
|
||||
// runner lacks, listing them in missing.
|
||||
Bind :: (bundle: *void, state: *RunnerState, bindings: *Vec(HostBinding), missing: *Vec(u8)) -> s64 {
|
||||
gaps: s64 = 0
|
||||
count := BundleCapabilities(bundle)
|
||||
index: u64 = 0
|
||||
while index < count {
|
||||
module := BundleCapabilityModuleName(bundle, index)
|
||||
function := BundleCapabilityFunctionName(bundle, index)
|
||||
if RunnerProvides(TextFromCString(module), TextFromCString(function)) {
|
||||
VecPush(<<bindings, HostBinding.{module = module, function = function, call = RunnerCall, context = cast(*void)state})
|
||||
} else {
|
||||
gaps += 1
|
||||
if gaps <= LISTED_GAPS {
|
||||
BuilderPrint(missing, "%", ifx gaps == 1 then " " else ", ")
|
||||
BuilderPrint(missing, "%:%", TextFromCString(module), TextFromCString(function))
|
||||
}
|
||||
}
|
||||
index += 1
|
||||
}
|
||||
if gaps > LISTED_GAPS { BuilderPrint(missing, " and % more", gaps - LISTED_GAPS) }
|
||||
return gaps
|
||||
}
|
||||
|
||||
// Runs the bound bundle natively and returns its exit status, or -1 when it
|
||||
// could not start or stopped with an error.
|
||||
Run :: (bundle: *void, bindings: []HostBinding) -> s32 {
|
||||
first: *HostBinding
|
||||
if bindings.count > 0 { first = *bindings[0] }
|
||||
options := BundleInstanceOptions.{backend = .Native, max_steps = 0}
|
||||
instance := InstantiateBundleWithOptions(bundle, first, cast(u64)bindings.count, *options)
|
||||
if instance == null { return -1 }
|
||||
value: s64
|
||||
has_value: s32
|
||||
finished := RunBundleInstance(instance, *value, *has_value)
|
||||
CloseBundleInstance(instance)
|
||||
if finished == 0 { return -1 }
|
||||
if has_value == 0 { return 0 }
|
||||
return cast(s32)(value & 255)
|
||||
}
|
||||
|
||||
// What the dialog says about a run that failed while unattended.
|
||||
Report :: (path: string, status: s32, detail: string) -> Text {
|
||||
report: Vec(u8)
|
||||
if status < 0 && detail == "" {
|
||||
BuilderPrint(*report, "% stopped with an error.", path)
|
||||
} else if status < 0 {
|
||||
BuilderPrint(*report, "% stopped with an error:\n\n%", path, detail)
|
||||
} else if detail == "" {
|
||||
BuilderPrint(*report, "% stopped with exit status %.", path, status)
|
||||
} else {
|
||||
BuilderPrint(*report, "% stopped with exit status %:\n\n%", path, status, detail)
|
||||
}
|
||||
return BuilderFinish(report)
|
||||
}
|
||||
|
||||
#scope_export
|
||||
#program_export
|
||||
main :: (argc: s32, argv: **u8) -> s32 {
|
||||
attended := TerminalRaw(2) == 1
|
||||
attended := TerminalRaw(STANDARD_ERROR) == 1
|
||||
if argc < 2 {
|
||||
return Fail("usage: zx FILE.zx [ARGUMENT...]: open a .zx program with ZX Runner", attended, 2)
|
||||
}
|
||||
@@ -82,49 +132,57 @@ main :: (argc: s32, argv: **u8) -> s32 {
|
||||
problem: Text = ZxHeaderMessage(path, header)
|
||||
return Fail(TextView(problem), attended, 1)
|
||||
}
|
||||
|
||||
directory_storage: [4096]u8
|
||||
directory := ToolDirectory(directory_storage[:])
|
||||
if directory == "" { return Fail("cannot find the Ziran toolchain beside this runner", attended, 1) }
|
||||
engine: Vec(u8)
|
||||
BuilderPrint(*engine, "%/zi2zx", directory)
|
||||
engine_path: Text = BuilderFinish(engine)
|
||||
// --host-dispatch makes the engine stop with GAP before running anything
|
||||
// when the bundle imports a capability it does not provide.
|
||||
arguments: Vec(string)
|
||||
VecPush(arguments, TextView(engine_path))
|
||||
VecPush(arguments, "run")
|
||||
VecPush(arguments, "--host-dispatch")
|
||||
for index: 1..argc - 1 { VecPush(arguments, TextFromCString(argv[index])) }
|
||||
|
||||
options: ProcessOptions = .{.arguments = VecSlice(arguments, 0, arguments.count)}
|
||||
log: Text = ErrorLogPath()
|
||||
if !attended { options.error_path = TextView(log) }
|
||||
run: ProcessRun
|
||||
started := RunProcess(options, *run)
|
||||
errors := ""
|
||||
if !attended {
|
||||
text, ok := ReadEntireFile(TextView(log))
|
||||
if ok { errors = text; unused PrintError(text) }
|
||||
unused RemoveFile(TextView(log))
|
||||
bundle := OpenBundle(argv[1])
|
||||
if bundle == null {
|
||||
damaged: Vec(u8)
|
||||
BuilderPrint(*damaged, "% is damaged: it has a ZX header but its program cannot be read", path)
|
||||
message: Text = BuilderFinish(damaged)
|
||||
return Fail(TextView(message), attended, 1)
|
||||
}
|
||||
if !started { return Fail("cannot start the Ziran toolchain beside this runner", attended, 1) }
|
||||
if run.code == GAP {
|
||||
defer CloseBundle(bundle)
|
||||
// The bundle sees its own path as argument 0, then the arguments after it.
|
||||
state := RunnerState.{bundle = bundle, argc = argc - 1, argv = argv}
|
||||
bindings: Vec(HostBinding)
|
||||
defer VecFree(bindings)
|
||||
missing: Vec(u8)
|
||||
if Bind(bundle, *state, *bindings, *missing) > 0 {
|
||||
gap: Vec(u8)
|
||||
BuilderPrint(*gap, "% needs platform capabilities, such as a window, that this ZX Runner does not provide", path)
|
||||
missing: Text = BuilderFinish(gap)
|
||||
return Fail(TextView(missing), attended, 1)
|
||||
BuilderPrint(*gap, "% needs capabilities this ZX Runner does not provide:%", path, TextFromBytes(VecSlice(missing, 0, missing.count)))
|
||||
message: Text = BuilderFinish(gap)
|
||||
return Fail(TextView(message), attended, 1)
|
||||
}
|
||||
if run.code != 0 && !attended {
|
||||
message: Vec(u8)
|
||||
detail := Tail(errors, 1500)
|
||||
if detail == "" {
|
||||
BuilderPrint(*message, "% stopped with exit status %.", path, run.code)
|
||||
} else {
|
||||
BuilderPrint(*message, "% stopped with exit status %:\n\n%", path, run.code, detail)
|
||||
native := GetBundleNativeProblem(bundle)
|
||||
if native != "" {
|
||||
unsupported: Vec(u8)
|
||||
BuilderPrint(*unsupported, "% cannot run on this computer: %", path, native)
|
||||
message: Text = BuilderFinish(unsupported)
|
||||
return Fail(TextView(message), attended, 1)
|
||||
}
|
||||
// Unattended, the engine's own diagnostics go to a file for the dialog.
|
||||
log: Text = ErrorLogPath()
|
||||
saved: s32 = -1
|
||||
if !attended {
|
||||
name: [4096]u8
|
||||
if CopyCString(TextView(log), name[:]) {
|
||||
capture := OpenRaw(*name[0], WRITE_TRUNCATE_CREATE, 384)
|
||||
if capture >= 0 {
|
||||
saved = DuplicateRaw(STANDARD_ERROR)
|
||||
unused ReplaceRaw(capture, STANDARD_ERROR)
|
||||
unused CloseRaw(capture)
|
||||
}
|
||||
}
|
||||
shown: Text = BuilderFinish(message)
|
||||
ShowError(TextView(shown))
|
||||
}
|
||||
return run.code
|
||||
status := Run(bundle, VecSlice(bindings, 0, bindings.count))
|
||||
if saved >= 0 {
|
||||
unused ReplaceRaw(saved, STANDARD_ERROR)
|
||||
unused CloseRaw(saved)
|
||||
errors, ok := ReadEntireFile(TextView(log))
|
||||
unused RemoveFile(TextView(log))
|
||||
if ok { unused PrintError(errors) }
|
||||
if status != 0 {
|
||||
shown: Text = Report(path, status, ifx ok then Tail(errors, 1500) else "")
|
||||
ShowError(TextView(shown))
|
||||
}
|
||||
}
|
||||
return ifx status < 0 then 1 else status
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
// The capabilities ZX Runner gives every bundle: its own assets, the clock,
|
||||
// the process arguments and environment, and files. A bundle reaches them
|
||||
// through std/guest_assets, guest_clock, guest_process and guest_files. Each
|
||||
// call answers in a host value; a string result stays valid until the
|
||||
// runner's next call.
|
||||
#import "std/bundle_host"
|
||||
#import "std/byte_text_linux"
|
||||
#import "std/c_string"
|
||||
#import "std/environment_linux"
|
||||
#import "std/file"
|
||||
#import "std/file_linux"
|
||||
#import "std/file_tree_linux"
|
||||
#import "std/timer_linux"
|
||||
using Vectors :: #import "std/vec"
|
||||
|
||||
RunnerState :: struct {
|
||||
bundle: *void
|
||||
// The bundle's arguments are argv[1] to argv[argc]: argv[0] is the runner.
|
||||
argc: s32
|
||||
argv: **u8
|
||||
// The bytes the latest string result borrows.
|
||||
answer: Vec(u8)
|
||||
}
|
||||
|
||||
// Whether this runner answers module:function.
|
||||
RunnerProvides :: (module: string, function: string) -> bool {
|
||||
if module == "guest_assets" {
|
||||
return function == "AssetCountHost" || function == "AssetNameHost" || function == "AssetDataHost"
|
||||
}
|
||||
if module == "guest_clock" { return function == "MonotonicNanosecondsHost" || function == "WallMillisecondsHost" }
|
||||
if module == "guest_process" {
|
||||
return function == "ArgumentCountHost" || function == "ArgumentHost" || function == "EnvironmentHost"
|
||||
}
|
||||
if module == "guest_files" {
|
||||
return function == "FileStatusHost" || function == "FileReadHost" || function == "FileWriteHost" ||
|
||||
function == "FileRemoveHost" || function == "FileRenameHost" ||
|
||||
function == "DirectoryMakeHost" || function == "DirectoryListHost"
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
#scope_file
|
||||
using HostValueKind;
|
||||
libc :: #system_library "libc";
|
||||
WallClock :: struct { seconds: s64; nanoseconds: s64 }
|
||||
ClockGetTime :: (clock: s32, value: *WallClock) -> s32 #foreign libc "clock_gettime";
|
||||
REALTIME_CLOCK :: 0;
|
||||
|
||||
Text :: (value: HostValue) -> string {
|
||||
if value.kind != cast(s32)HostString || value.length == 0 { return "" }
|
||||
return TextFromBytes(BytesFromPointer(cast(*u8)value.data, cast(u64)value.length))
|
||||
}
|
||||
|
||||
Whole :: (value: HostValue) -> s64 {
|
||||
if value.kind == cast(s32)HostUnsigned { return cast(s64)value.bits }
|
||||
return value.integer
|
||||
}
|
||||
|
||||
Integer :: (result: *HostValue, value: s64) -> s32 {
|
||||
result.kind = cast(s32)HostInteger; result.integer = value
|
||||
return 1
|
||||
}
|
||||
|
||||
Unsigned :: (result: *HostValue, value: u64) -> s32 {
|
||||
result.kind = cast(s32)HostUnsigned; result.bits = value
|
||||
return 1
|
||||
}
|
||||
|
||||
Borrowed :: (result: *HostValue, data: *u8, length: u64) -> s32 {
|
||||
result.kind = cast(s32)HostString; result.data = data; result.length = cast(usize)length
|
||||
return 1
|
||||
}
|
||||
|
||||
// Copies text into the runner's answer storage and returns it.
|
||||
Answer :: (state: *RunnerState, result: *HostValue, text: string) -> s32 {
|
||||
VecClear(state.answer)
|
||||
for byte: text { VecPush(state.answer, byte) }
|
||||
if state.answer.count == 0 { return Borrowed(result, null, 0) }
|
||||
return Borrowed(result, *state.answer[0], cast(u64)state.answer.count)
|
||||
}
|
||||
|
||||
AssetCall :: (state: *RunnerState, function: string, arguments: *HostValue, count: s32, result: *HostValue) -> s32 {
|
||||
if function == "AssetCountHost" && count == 0 { return Unsigned(result, BundleAssets(state.bundle)) }
|
||||
if count != 1 { return 0 }
|
||||
index := cast(u64)Whole(arguments[0])
|
||||
if index >= BundleAssets(state.bundle) { return Borrowed(result, null, 0) }
|
||||
if function == "AssetNameHost" {
|
||||
name := TextFromCString(BundleAssetPath(state.bundle, index))
|
||||
return Answer(state, result, name)
|
||||
}
|
||||
if function == "AssetDataHost" {
|
||||
return Borrowed(result, BundleAssetBytes(state.bundle, index), BundleAssetLength(state.bundle, index))
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
ClockCall :: (function: string, count: s32, result: *HostValue) -> s32 {
|
||||
if count != 0 { return 0 }
|
||||
if function == "MonotonicNanosecondsHost" { return Integer(result, MonotonicNanoseconds()) }
|
||||
if function == "WallMillisecondsHost" {
|
||||
now: WallClock
|
||||
if ClockGetTime(REALTIME_CLOCK, *now) != 0 { return 0 }
|
||||
return Integer(result, now.seconds * 1000 + now.nanoseconds / 1000000)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
ProcessCall :: (state: *RunnerState, function: string, arguments: *HostValue, count: s32, result: *HostValue) -> s32 {
|
||||
if function == "ArgumentCountHost" && count == 0 { return Integer(result, cast(s64)state.argc) }
|
||||
if count != 1 { return 0 }
|
||||
if function == "ArgumentHost" {
|
||||
index := Whole(arguments[0])
|
||||
if index < 0 || index >= cast(s64)state.argc { return Borrowed(result, null, 0) }
|
||||
return Answer(state, result, TextFromCString(state.argv[index + 1]))
|
||||
}
|
||||
if function == "EnvironmentHost" { return Answer(state, result, EnvironmentText(Text(arguments[0]))) }
|
||||
return 0
|
||||
}
|
||||
|
||||
FileCall :: (state: *RunnerState, function: string, arguments: *HostValue, count: s32, result: *HostValue) -> s32 {
|
||||
if count < 1 { return 0 }
|
||||
path := Text(arguments[0])
|
||||
if function == "FileStatusHost" && count == 1 {
|
||||
info := PathStatus(path)
|
||||
if info.kind == .Missing { return Integer(result, -1) }
|
||||
if info.kind == .Directory { return Integer(result, -2) }
|
||||
return Integer(result, info.size)
|
||||
}
|
||||
if function == "FileReadHost" && count == 1 {
|
||||
text, ok := ReadEntireFile(path)
|
||||
if !ok { return Borrowed(result, null, 0) }
|
||||
return Answer(state, result, text)
|
||||
}
|
||||
if function == "FileWriteHost" && count == 2 {
|
||||
return Integer(result, ifx WriteEntireFile(path, Text(arguments[1])) then 1 else 0)
|
||||
}
|
||||
if function == "FileRemoveHost" && count == 1 { return Integer(result, ifx RemoveFile(path) then 1 else 0) }
|
||||
if function == "FileRenameHost" && count == 2 {
|
||||
return Integer(result, ifx RenamePath(path, Text(arguments[1])) then 1 else 0)
|
||||
}
|
||||
if function == "DirectoryMakeHost" && count == 1 { return Integer(result, ifx CreateDirectory(path) then 1 else 0) }
|
||||
if function == "DirectoryListHost" && count == 1 {
|
||||
listing: Vec(u8)
|
||||
cursor := OpenDirectory(path)
|
||||
if cursor.valid {
|
||||
while true {
|
||||
entry := NextDirectoryEntry(*cursor)
|
||||
if !entry.valid { break }
|
||||
name := TextFromBytes(entry.name[0:entry.name_count])
|
||||
if name == "." || name == ".." { continue }
|
||||
if listing.count > 0 { VecPush(listing, #char "\n") }
|
||||
for byte: name { VecPush(listing, byte) }
|
||||
}
|
||||
unused CloseDirectory(*cursor)
|
||||
}
|
||||
return Answer(state, result, TextFromBytes(VecSlice(listing, 0, listing.count)))
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
#scope_export
|
||||
// The one host callback the runner binds for every capability it provides.
|
||||
RunnerCall :: (context: *void, module_name: *u8, function_name: *u8,
|
||||
arguments: *HostValue, count: s32, result: *HostValue) -> s32 {
|
||||
state := cast(*RunnerState)context
|
||||
module := TextFromCString(module_name)
|
||||
function := TextFromCString(function_name)
|
||||
if !RunnerProvides(module, function) { return 0 }
|
||||
result.kind = cast(s32)HostVoid
|
||||
if module == "guest_assets" { return AssetCall(state, function, arguments, count, result) }
|
||||
if module == "guest_clock" { return ClockCall(function, count, result) }
|
||||
if module == "guest_process" { return ProcessCall(state, function, arguments, count, result) }
|
||||
if module == "guest_files" { return FileCall(state, function, arguments, count, result) }
|
||||
return 0
|
||||
}
|
||||
@@ -1,11 +1,16 @@
|
||||
// The first check ZX Runner makes: is this a .zx program this toolchain
|
||||
// reads? The header layout is ASSEMBLY/zx-executable-format.md; the full
|
||||
// container is verified again when the program loads.
|
||||
#import "compiler_bundle"
|
||||
#import "std/file_linux"
|
||||
#import "std/text_tools"
|
||||
using Vectors :: #import "std/vec"
|
||||
|
||||
// The ZX revision the linked runtime reads (ASSEMBLY/zx-executable-format.md,
|
||||
// ExecutableVersion in compiler_bundle.zi). The runtime is linked into the
|
||||
// runner, so it cannot also import that module; the install_user check runs
|
||||
// a bundle the toolchain just wrote and fails when the two differ.
|
||||
ZX_FORMAT_REVISION :: 28;
|
||||
|
||||
ZxHeaderKind :: enum s32 { Runnable; Missing; NotZx; Older; Newer }
|
||||
ZxHeader :: struct { kind: ZxHeaderKind; revision: u32 }
|
||||
|
||||
@@ -19,7 +24,7 @@ ReadZxHeader :: (path: string) -> ZxHeader {
|
||||
return .{kind = .NotZx}
|
||||
}
|
||||
revision := cast(u32)bytes[4] | (cast(u32)bytes[5] << 8) | (cast(u32)bytes[6] << 16) | (cast(u32)bytes[7] << 24)
|
||||
current := ExecutableVersionNumber()
|
||||
current: u32 = ZX_FORMAT_REVISION
|
||||
if revision > current { return .{kind = .Newer, revision = revision} }
|
||||
if revision < current { return .{kind = .Older, revision = revision} }
|
||||
return .{kind = .Runnable, revision = revision}
|
||||
@@ -27,7 +32,7 @@ ReadZxHeader :: (path: string) -> ZxHeader {
|
||||
|
||||
ZxHeaderMessage :: (path: string, header: ZxHeader) -> Text {
|
||||
message: Vec(u8)
|
||||
current := ExecutableVersionNumber()
|
||||
current: u32 = ZX_FORMAT_REVISION
|
||||
if header.kind == .Missing {
|
||||
BuilderPrint(*message, "cannot open %: there is no readable file there", path)
|
||||
} else if header.kind == .NotZx {
|
||||
|
||||
+20
-6
@@ -281,12 +281,26 @@ UI code and embedded assets; the installed provider bridges the local OS.
|
||||
### ZX Runner
|
||||
|
||||
`make install-user` installs `zx`, ZX Runner, beside the toolchain. `zx
|
||||
app.zx [ARGUMENT...]` runs any bundle with that toolchain's runtime, as Linux
|
||||
runs an ELF file; a console program and a window program are opened the same
|
||||
way. The bundle carries all of its code, including the libraries it imports,
|
||||
and imports only the host capabilities the runner provides. A capability the
|
||||
runner lacks is an error naming the bundle; unlike `ziran run`, the runner
|
||||
hands a bundle to no installed provider. It reads the header first, so a file
|
||||
app.zx [ARGUMENT...]` runs any bundle in-process with the linked runtime, as
|
||||
Linux runs an ELF file; a console program and a window program are opened the
|
||||
same way. The bundle carries all of its code, including the libraries it
|
||||
imports, and imports only host capabilities (`module:function`), which the
|
||||
runner answers itself. It names every capability it lacks and runs nothing;
|
||||
unlike `ziran run`, it hands a bundle to no installed provider.
|
||||
|
||||
The capabilities every bundle may use are declared in standard modules:
|
||||
|
||||
| Module | Capabilities |
|
||||
| --- | --- |
|
||||
| `std/guest_assets` | `AssetCount`, `AssetName`, `AssetData`, `AssetFind`: the bundle's own embedded files |
|
||||
| `std/guest_clock` | `MonotonicNanoseconds`, `WallMilliseconds` (since 1970 UTC) |
|
||||
| `std/guest_files` | `FileStatus` (size, `FILE_MISSING`, `FILE_DIRECTORY`), `FileRead`, `FileWrite`, `FileRemove`, `FileRename`, `DirectoryMake`, `DirectoryList`, with a native program's reach |
|
||||
| `std/guest_process` | `ArgumentCount`, `Argument` (0 is the bundle's path), `Environment` |
|
||||
|
||||
Capabilities are only added: a runner never changes what an existing name
|
||||
means, and a different meaning gets a new name.
|
||||
|
||||
`zx` reads the header first, so a file
|
||||
that is not a ZX program, or one written in an older or newer ZX format
|
||||
revision, gets a plain explanation naming both revisions. Started without a
|
||||
terminal on standard error, as a file manager starts it, ZX Runner also shows
|
||||
|
||||
@@ -32,13 +32,33 @@ modular arcihetcuer".
|
||||
(`packaging/linux`, `make install-user`), checked by `install_user`.
|
||||
Open: generated app and file-type icons, exported to hicolor 16-512 and
|
||||
Android adaptive layers.
|
||||
2. Window capabilities: their versioned interface in Ziran with docs and
|
||||
tests, the desktop host behind it linked into `zx`, and Kryon's desktop
|
||||
backend ported onto it, so a Kryon example `.zx` runs through plain `zx`
|
||||
on a private Xvfb. The surface to cover is what Kryon's player answers
|
||||
today (`kryon/src/backend/desktop_zx.zi` `Supported`): raster shapes, text,
|
||||
triangles and images, glyph metrics, bitmaps, cursor, window state, input
|
||||
and text entry, clipboard, media and video, clock, theme colour.
|
||||
2. Capabilities through the runner, with no compiler change. A capability is
|
||||
a `#foreign host_api` function in a Ziran std module; a bundle lists the
|
||||
ones it imports (`module:function`), and `zx` binds each from its own table
|
||||
through `std/bundle_host` or names the ones it lacks and stops. Today a
|
||||
bundle has only `print`: `std/file_linux` has no portable lowering, so a
|
||||
`.zx` cannot even read a file.
|
||||
a. Done: `zx` hosts the bundle in-process (`std/bundle_host`, native
|
||||
backend) and binds imports from its table (cmd/zx_runner_capabilities.zi).
|
||||
b. Done: the base set in `std/guest_assets`, `guest_clock`, `guest_files`
|
||||
and `guest_process` (docs/ZX.md "ZX Runner"). Open: `std/file` and its
|
||||
neighbours use them when `TARGET == .ZX`, so ordinary code runs in a
|
||||
bundle unchanged.
|
||||
c. Window set: open a window (title, size, flags), size and scale,
|
||||
present a premultiplied RGBA frame with its damage rectangle, input
|
||||
events (pointer, wheel, touch, keys with modifiers, text, resize,
|
||||
focus, close), cursor shape or picture, clipboard text, text input on
|
||||
and off, frame pacing; audio output follows. The runner calls the
|
||||
bundle's frame entry once per frame. Drawing, text and images run in
|
||||
the bundle. Desktop host: SDL2 inside `zx`.
|
||||
d. Proof: a plain Ziran example that draws and reacts to input runs
|
||||
through `zx` on a private Xvfb.
|
||||
e. Kryon's zx backend: its pixmap rasterizer and font decoder compiled
|
||||
into the bundle, presenting through the window set, so a Kryon example
|
||||
runs through plain `zx`. Kryon's player answers 62 capabilities on
|
||||
desktop and 46 on Android today (`kryon/src/backend/desktop_zx.zi` and
|
||||
`bundle_player.zi` `Supported`); video and system theme become
|
||||
optional capabilities.
|
||||
3. Every Kryon app (Breeze, Werk, Jungle and the rest) builds for that runner;
|
||||
Kryon's players (desktop_zx, app_player, window_player, zx_window_*,
|
||||
bundle_player*, android_bundle_run) and the player copies
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@
|
||||
<a href="examples.html"><span>Learn by doing</span><strong>Examples</strong><p><!-- generated:example_count -->16<!-- /generated:example_count --> complete programs with commands, each printing the same output on every target.</p><b>Browse examples →</b></a>
|
||||
<a href="language.html"><span>Language</span><strong>Syntax reference</strong><p>Modules, declarations, types, generics, control flow, compile-time forms, laws, and foreign calls.</p><b>Read the reference →</b></a>
|
||||
<a href="api.html#toolchain"><span>Toolchain</span><strong>CLI reference</strong><p>Check, build C or Go, bundle, run, format, and query the toolchain.</p><b>See commands →</b></a>
|
||||
<a href="modules.html"><span>Libraries</span><strong>Modules and packages</strong><p>Every public declaration in the <!-- generated:std_count -->132<!-- /generated:std_count --> standard modules and the ziranlang packages, generated from the source.</p><b>Browse modules →</b></a>
|
||||
<a href="modules.html"><span>Libraries</span><strong>Modules and packages</strong><p>Every public declaration in the <!-- generated:std_count -->136<!-- /generated:std_count --> standard modules and the ziranlang packages, generated from the source.</p><b>Browse modules →</b></a>
|
||||
<a href="status.html"><span>Project</span><strong>Implementation status</strong><p>See what works in the tested subset and what still needs implementation.</p><b>Check support →</b></a>
|
||||
<a href="guide/packages.html"><span>Projects</span><strong>Packages</strong><p><code>ziran.toml</code> projects, pinned Git dependencies, locks, and installing applications.</p><b>Read the guide →</b></a>
|
||||
</div></section>
|
||||
|
||||
+36
-6
@@ -247,12 +247,42 @@ a manifest or generated native output. The portable bundle carries its linked
|
||||
UI code and embedded assets; the installed provider bridges the local OS.</p>
|
||||
<h3 id="zx-runner">ZX Runner</h3>
|
||||
<p><code>make install-user</code> installs <code>zx</code>, ZX Runner, beside the toolchain. <code>zx
|
||||
app.zx [ARGUMENT...]</code> runs any bundle with that toolchain's runtime, as Linux
|
||||
runs an ELF file; a console program and a window program are opened the same
|
||||
way. The bundle carries all of its code, including the libraries it imports,
|
||||
and imports only the host capabilities the runner provides. A capability the
|
||||
runner lacks is an error naming the bundle; unlike <code>ziran run</code>, the runner
|
||||
hands a bundle to no installed provider. It reads the header first, so a file
|
||||
app.zx [ARGUMENT...]</code> runs any bundle in-process with the linked runtime, as
|
||||
Linux runs an ELF file; a console program and a window program are opened the
|
||||
same way. The bundle carries all of its code, including the libraries it
|
||||
imports, and imports only host capabilities (<code>module:function</code>), which the
|
||||
runner answers itself. It names every capability it lacks and runs nothing;
|
||||
unlike <code>ziran run</code>, it hands a bundle to no installed provider.</p>
|
||||
<p>The capabilities every bundle may use are declared in standard modules:</p>
|
||||
<div class="table-scroll"><table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Module</th>
|
||||
<th>Capabilities</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td><code>std/guest_assets</code></td>
|
||||
<td><code>AssetCount</code>, <code>AssetName</code>, <code>AssetData</code>, <code>AssetFind</code>: the bundle's own embedded files</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>std/guest_clock</code></td>
|
||||
<td><code>MonotonicNanoseconds</code>, <code>WallMilliseconds</code> (since 1970 UTC)</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>std/guest_files</code></td>
|
||||
<td><code>FileStatus</code> (size, <code>FILE_MISSING</code>, <code>FILE_DIRECTORY</code>), <code>FileRead</code>, <code>FileWrite</code>, <code>FileRemove</code>, <code>FileRename</code>, <code>DirectoryMake</code>, <code>DirectoryList</code>, with a native program's reach</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>std/guest_process</code></td>
|
||||
<td><code>ArgumentCount</code>, <code>Argument</code> (0 is the bundle's path), <code>Environment</code></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table></div>
|
||||
<p>Capabilities are only added: a runner never changes what an existing name
|
||||
means, and a different meaning gets a new name.</p>
|
||||
<p><code>zx</code> reads the header first, so a file
|
||||
that is not a ZX program, or one written in an older or newer ZX format
|
||||
revision, gets a plain explanation naming both revisions. Started without a
|
||||
terminal on standard error, as a file manager starts it, ZX Runner also shows
|
||||
|
||||
@@ -96,6 +96,10 @@
|
||||
<url><loc>https://ziran-lang.org/std/go_types</loc></url>
|
||||
<url><loc>https://ziran-lang.org/std/grapheme</loc></url>
|
||||
<url><loc>https://ziran-lang.org/std/grapheme_data</loc></url>
|
||||
<url><loc>https://ziran-lang.org/std/guest_assets</loc></url>
|
||||
<url><loc>https://ziran-lang.org/std/guest_clock</loc></url>
|
||||
<url><loc>https://ziran-lang.org/std/guest_files</loc></url>
|
||||
<url><loc>https://ziran-lang.org/std/guest_process</loc></url>
|
||||
<url><loc>https://ziran-lang.org/std/gzip_linux</loc></url>
|
||||
<url><loc>https://ziran-lang.org/std/hash_map</loc></url>
|
||||
<url><loc>https://ziran-lang.org/std/hmac_sha256_go</loc></url>
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="theme-color" content="#f7f3eb"><meta name="description" content="The files embedded in the running bundle (`ziran bundle --asset-dir`), read through its runner."><title>std/guest_assets · Ziran</title><link rel="canonical" href="https://ziran-lang.org/std/guest_assets"><link rel="icon" href="../assets/logo-capybara.png" type="image/png"><link rel="stylesheet" href="../styles.css"><script defer src="../site.js"></script></head>
|
||||
<body><a class="skip-link" href="#main">Skip to content</a><header class="site-header"><div class="site-header-inner"><a class="brand" href="../index.html"><img class="brand-icon" src="../assets/logo-capybara.png" alt="" width="36" height="36"><span>Ziran</span></a><button class="menu-button" type="button" aria-expanded="false" aria-controls="primary-nav">Menu</button><nav class="header-nav" id="primary-nav" aria-label="Primary"><a href="../get-started.html">Get started</a><a href="../examples.html">Examples</a><a href="../targets.html">Targets</a><a href="../modules.html" aria-current="page">Modules</a><a href="../docs.html">Docs</a><a href="../status.html">Status</a><a href="https://github.com/ziranlang/ziran">GitHub ↗</a><button class="theme-toggle" type="button" aria-label="Switch color theme"></button></nav></div></header>
|
||||
<main id="main" class="article-layout"><div class="article-head"><p class="eyebrow"><a href="../modules.html#std">Modules</a> / Standard library</p><h1>std/guest_assets</h1><p>The files embedded in the running bundle (`ziran bundle --asset-dir`), read through its runner. Names are canonical relative paths; the bytes stay valid while the bundle runs.</p><p class="module-meta"><span class="status-badge is-in-progress">In progress</span> <span class="module-target">Needs: Host capability.</span> Available on master (Unreleased).</p></div>
|
||||
<div class="article-grid"><aside class="page-toc"><strong>Declarations</strong><a href="#AssetCount">AssetCount</a><a href="#AssetName">AssetName</a><a href="#AssetData">AssetData</a><a href="#AssetFind">AssetFind</a></aside><div class="article-body api-body">
|
||||
<section><p>Import with <code>#import "std/guest_assets"</code>.</p><div class="api-list">
|
||||
<div id="AssetCount"><code>AssetCount :: () -> u64</code><p class="api-note">Needs a host capability.</p></div>
|
||||
<div id="AssetName"><code>AssetName :: (index: u64) -> string</code><p class="api-note">Needs a host capability.</p></div>
|
||||
<div id="AssetData"><code>AssetData :: (index: u64) -> string</code><p class="api-note">Needs a host capability.</p></div>
|
||||
<div id="AssetFind"><code>AssetFind :: (name: string) -> (string, bool)</code><p>The bytes of the asset called name, or ok false when there is none.</p><p class="api-note">Needs a host capability.</p></div>
|
||||
</div><p><a class="text-link" href="https://github.com/ziranlang/ziran/blob/master/std/guest_assets.zi">Source ↗</a> · <a class="text-link" href="../modules.html#std">All modules</a></p></section>
|
||||
</div></div></main>
|
||||
<footer><div class="footer-top"><a class="brand" href="../index.html"><img class="brand-icon" src="../assets/logo-capybara.png" alt="" width="36" height="36"><span>Ziran</span></a><nav aria-label="Footer"><a href="../get-started.html">Get started</a><a href="../examples.html">Examples</a><a href="../targets.html">Targets</a><a href="../modules.html">Modules</a><a href="../docs.html">Docs</a><a href="../status.html">Status</a><a href="../index.html#support">Support</a><a href="https://github.com/ziranlang/ziran">GitHub ↗</a></nav></div><div class="footer-bottom"><p>A language for useful software.</p><p>© 2026 Kryon Labs · <a href="https://waozi.xyz/">Made by Waozi</a></p></div></footer></body></html>
|
||||
@@ -0,0 +1,12 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="theme-color" content="#f7f3eb"><meta name="description" content="Time for a portable bundle, supplied by the program that runs it, such as ZX Runner (`zx`)."><title>std/guest_clock · Ziran</title><link rel="canonical" href="https://ziran-lang.org/std/guest_clock"><link rel="icon" href="../assets/logo-capybara.png" type="image/png"><link rel="stylesheet" href="../styles.css"><script defer src="../site.js"></script></head>
|
||||
<body><a class="skip-link" href="#main">Skip to content</a><header class="site-header"><div class="site-header-inner"><a class="brand" href="../index.html"><img class="brand-icon" src="../assets/logo-capybara.png" alt="" width="36" height="36"><span>Ziran</span></a><button class="menu-button" type="button" aria-expanded="false" aria-controls="primary-nav">Menu</button><nav class="header-nav" id="primary-nav" aria-label="Primary"><a href="../get-started.html">Get started</a><a href="../examples.html">Examples</a><a href="../targets.html">Targets</a><a href="../modules.html" aria-current="page">Modules</a><a href="../docs.html">Docs</a><a href="../status.html">Status</a><a href="https://github.com/ziranlang/ziran">GitHub ↗</a><button class="theme-toggle" type="button" aria-label="Switch color theme"></button></nav></div></header>
|
||||
<main id="main" class="article-layout"><div class="article-head"><p class="eyebrow"><a href="../modules.html#std">Modules</a> / Standard library</p><h1>std/guest_clock</h1><p>Time for a portable bundle, supplied by the program that runs it, such as ZX Runner (`zx`). A runner that lacks a capability refuses the bundle.</p><p class="module-meta"><span class="status-badge is-in-progress">In progress</span> <span class="module-target">Needs: Host capability.</span> Available on master (Unreleased).</p></div>
|
||||
<div class="article-grid"><aside class="page-toc"><strong>Declarations</strong><a href="#MonotonicNanoseconds">MonotonicNanoseconds</a><a href="#WallMilliseconds">WallMilliseconds</a></aside><div class="article-body api-body">
|
||||
<section><p>Import with <code>#import "std/guest_clock"</code>.</p><div class="api-list">
|
||||
<div id="MonotonicNanoseconds"><code>MonotonicNanoseconds :: () -> s64</code><p>Nanoseconds from an arbitrary start; never goes backwards.</p><p class="api-note">Needs a host capability.</p></div>
|
||||
<div id="WallMilliseconds"><code>WallMilliseconds :: () -> s64</code><p>Milliseconds since 1970-01-01 UTC.</p><p class="api-note">Needs a host capability.</p></div>
|
||||
</div><p><a class="text-link" href="https://github.com/ziranlang/ziran/blob/master/std/guest_clock.zi">Source ↗</a> · <a class="text-link" href="../modules.html#std">All modules</a></p></section>
|
||||
</div></div></main>
|
||||
<footer><div class="footer-top"><a class="brand" href="../index.html"><img class="brand-icon" src="../assets/logo-capybara.png" alt="" width="36" height="36"><span>Ziran</span></a><nav aria-label="Footer"><a href="../get-started.html">Get started</a><a href="../examples.html">Examples</a><a href="../targets.html">Targets</a><a href="../modules.html">Modules</a><a href="../docs.html">Docs</a><a href="../status.html">Status</a><a href="../index.html#support">Support</a><a href="https://github.com/ziranlang/ziran">GitHub ↗</a></nav></div><div class="footer-bottom"><p>A language for useful software.</p><p>© 2026 Kryon Labs · <a href="https://waozi.xyz/">Made by Waozi</a></p></div></footer></body></html>
|
||||
@@ -0,0 +1,19 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="theme-color" content="#f7f3eb"><meta name="description" content="Files for a portable bundle, read and written by the program that runs it, such as ZX Runner, with the same reach as a native program."><title>std/guest_files · Ziran</title><link rel="canonical" href="https://ziran-lang.org/std/guest_files"><link rel="icon" href="../assets/logo-capybara.png" type="image/png"><link rel="stylesheet" href="../styles.css"><script defer src="../site.js"></script></head>
|
||||
<body><a class="skip-link" href="#main">Skip to content</a><header class="site-header"><div class="site-header-inner"><a class="brand" href="../index.html"><img class="brand-icon" src="../assets/logo-capybara.png" alt="" width="36" height="36"><span>Ziran</span></a><button class="menu-button" type="button" aria-expanded="false" aria-controls="primary-nav">Menu</button><nav class="header-nav" id="primary-nav" aria-label="Primary"><a href="../get-started.html">Get started</a><a href="../examples.html">Examples</a><a href="../targets.html">Targets</a><a href="../modules.html" aria-current="page">Modules</a><a href="../docs.html">Docs</a><a href="../status.html">Status</a><a href="https://github.com/ziranlang/ziran">GitHub ↗</a><button class="theme-toggle" type="button" aria-label="Switch color theme"></button></nav></div></header>
|
||||
<main id="main" class="article-layout"><div class="article-head"><p class="eyebrow"><a href="../modules.html#std">Modules</a> / Standard library</p><h1>std/guest_files</h1><p>Files for a portable bundle, read and written by the program that runs it, such as ZX Runner, with the same reach as a native program.</p><p class="module-meta"><span class="status-badge is-in-progress">In progress</span> <span class="module-target">Needs: Host capability.</span> Available on master (Unreleased).</p></div>
|
||||
<div class="article-grid"><aside class="page-toc"><strong>Declarations</strong><a href="#FILE_MISSING">FILE_MISSING</a><a href="#FILE_DIRECTORY">FILE_DIRECTORY</a><a href="#FileStatus">FileStatus</a><a href="#FileRead">FileRead</a><a href="#FileWrite">FileWrite</a><a href="#FileRemove">FileRemove</a><a href="#FileRename">FileRename</a><a href="#DirectoryMake">DirectoryMake</a><a href="#DirectoryList">DirectoryList</a></aside><div class="article-body api-body">
|
||||
<section><p>Import with <code>#import "std/guest_files"</code>.</p><div class="api-list">
|
||||
<div id="FILE_MISSING"><code>FILE_MISSING :: -1</code></div>
|
||||
<div id="FILE_DIRECTORY"><code>FILE_DIRECTORY :: -2</code></div>
|
||||
<div id="FileStatus"><code>FileStatus :: (path: string) -> s64</code><p class="api-note">Needs a host capability.</p></div>
|
||||
<div id="FileRead"><code>FileRead :: (path: string) -> (string, bool)</code><p>The whole file, or ok false when it is missing, a directory or unreadable.</p><p class="api-note">Needs a host capability.</p></div>
|
||||
<div id="FileWrite"><code>FileWrite :: (path: string, data: string) -> bool</code><p>Replaces the file with data; false when it could not be written.</p><p class="api-note">Needs a host capability.</p></div>
|
||||
<div id="FileRemove"><code>FileRemove :: (path: string) -> bool</code><p class="api-note">Needs a host capability.</p></div>
|
||||
<div id="FileRename"><code>FileRename :: (source: string, destination: string) -> bool</code><p class="api-note">Needs a host capability.</p></div>
|
||||
<div id="DirectoryMake"><code>DirectoryMake :: (path: string) -> bool</code><p class="api-note">Needs a host capability.</p></div>
|
||||
<div id="DirectoryList"><code>DirectoryList :: (path: string) -> string</code><p class="api-note">Needs a host capability.</p></div>
|
||||
</div><p><a class="text-link" href="https://github.com/ziranlang/ziran/blob/master/std/guest_files.zi">Source ↗</a> · <a class="text-link" href="../modules.html#std">All modules</a></p></section>
|
||||
</div></div></main>
|
||||
<footer><div class="footer-top"><a class="brand" href="../index.html"><img class="brand-icon" src="../assets/logo-capybara.png" alt="" width="36" height="36"><span>Ziran</span></a><nav aria-label="Footer"><a href="../get-started.html">Get started</a><a href="../examples.html">Examples</a><a href="../targets.html">Targets</a><a href="../modules.html">Modules</a><a href="../docs.html">Docs</a><a href="../status.html">Status</a><a href="../index.html#support">Support</a><a href="https://github.com/ziranlang/ziran">GitHub ↗</a></nav></div><div class="footer-bottom"><p>A language for useful software.</p><p>© 2026 Kryon Labs · <a href="https://waozi.xyz/">Made by Waozi</a></p></div></footer></body></html>
|
||||
@@ -0,0 +1,13 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="theme-color" content="#f7f3eb"><meta name="description" content="The arguments and environment a runner passes a portable bundle."><title>std/guest_process · Ziran</title><link rel="canonical" href="https://ziran-lang.org/std/guest_process"><link rel="icon" href="../assets/logo-capybara.png" type="image/png"><link rel="stylesheet" href="../styles.css"><script defer src="../site.js"></script></head>
|
||||
<body><a class="skip-link" href="#main">Skip to content</a><header class="site-header"><div class="site-header-inner"><a class="brand" href="../index.html"><img class="brand-icon" src="../assets/logo-capybara.png" alt="" width="36" height="36"><span>Ziran</span></a><button class="menu-button" type="button" aria-expanded="false" aria-controls="primary-nav">Menu</button><nav class="header-nav" id="primary-nav" aria-label="Primary"><a href="../get-started.html">Get started</a><a href="../examples.html">Examples</a><a href="../targets.html">Targets</a><a href="../modules.html" aria-current="page">Modules</a><a href="../docs.html">Docs</a><a href="../status.html">Status</a><a href="https://github.com/ziranlang/ziran">GitHub ↗</a><button class="theme-toggle" type="button" aria-label="Switch color theme"></button></nav></div></header>
|
||||
<main id="main" class="article-layout"><div class="article-head"><p class="eyebrow"><a href="../modules.html#std">Modules</a> / Standard library</p><h1>std/guest_process</h1><p>The arguments and environment a runner passes a portable bundle. Argument 0 is the bundle's path; `zx app.zx a b` gives the bundle three arguments.</p><p class="module-meta"><span class="status-badge is-in-progress">In progress</span> <span class="module-target">Needs: Host capability.</span> Available on master (Unreleased).</p></div>
|
||||
<div class="article-grid"><aside class="page-toc"><strong>Declarations</strong><a href="#ArgumentCount">ArgumentCount</a><a href="#Argument">Argument</a><a href="#Environment">Environment</a></aside><div class="article-body api-body">
|
||||
<section><p>Import with <code>#import "std/guest_process"</code>.</p><div class="api-list">
|
||||
<div id="ArgumentCount"><code>ArgumentCount :: () -> s32</code><p class="api-note">Needs a host capability.</p></div>
|
||||
<div id="Argument"><code>Argument :: (index: s32) -> string</code><p>The argument at index, or "" outside 0..ArgumentCount() - 1.</p><p class="api-note">Needs a host capability.</p></div>
|
||||
<div id="Environment"><code>Environment :: (name: string) -> string</code><p>The variable's value, or "" when it is unset.</p><p class="api-note">Needs a host capability.</p></div>
|
||||
</div><p><a class="text-link" href="https://github.com/ziranlang/ziran/blob/master/std/guest_process.zi">Source ↗</a> · <a class="text-link" href="../modules.html#std">All modules</a></p></section>
|
||||
</div></div></main>
|
||||
<footer><div class="footer-top"><a class="brand" href="../index.html"><img class="brand-icon" src="../assets/logo-capybara.png" alt="" width="36" height="36"><span>Ziran</span></a><nav aria-label="Footer"><a href="../get-started.html">Get started</a><a href="../examples.html">Examples</a><a href="../targets.html">Targets</a><a href="../modules.html">Modules</a><a href="../docs.html">Docs</a><a href="../status.html">Status</a><a href="../index.html#support">Support</a><a href="https://github.com/ziranlang/ziran">GitHub ↗</a></nav></div><div class="footer-bottom"><p>A language for useful software.</p><p>© 2026 Kryon Labs · <a href="https://waozi.xyz/">Made by Waozi</a></p></div></footer></body></html>
|
||||
@@ -0,0 +1,29 @@
|
||||
// The files embedded in the running bundle (`ziran bundle --asset-dir`),
|
||||
// read through its runner. Names are canonical relative paths; the bytes
|
||||
// stay valid while the bundle runs.
|
||||
host_api :: #system_library "host_api";
|
||||
|
||||
AssetCountHost :: () -> u64 #foreign host_api;
|
||||
AssetNameHost :: (index: u64) -> string #foreign host_api;
|
||||
AssetDataHost :: (index: u64) -> string #foreign host_api;
|
||||
|
||||
#program_export
|
||||
AssetCount :: () -> u64 { return AssetCountHost() }
|
||||
|
||||
#program_export
|
||||
AssetName :: (index: u64) -> string { return AssetNameHost(index) }
|
||||
|
||||
#program_export
|
||||
AssetData :: (index: u64) -> string { return AssetDataHost(index) }
|
||||
|
||||
// The bytes of the asset called name, or ok false when there is none.
|
||||
#program_export
|
||||
AssetFind :: (name: string) -> (data: string, ok: bool) {
|
||||
count := AssetCountHost()
|
||||
index: u64 = 0
|
||||
while index < count {
|
||||
if AssetNameHost(index) == name { return AssetDataHost(index), true }
|
||||
index += 1
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// Time for a portable bundle, supplied by the program that runs it, such as
|
||||
// ZX Runner (`zx`). A runner that lacks a capability refuses the bundle.
|
||||
host_api :: #system_library "host_api";
|
||||
|
||||
MonotonicNanosecondsHost :: () -> s64 #foreign host_api;
|
||||
WallMillisecondsHost :: () -> s64 #foreign host_api;
|
||||
|
||||
// Nanoseconds from an arbitrary start; never goes backwards.
|
||||
#program_export
|
||||
MonotonicNanoseconds :: () -> s64 { return MonotonicNanosecondsHost() }
|
||||
|
||||
// Milliseconds since 1970-01-01 UTC.
|
||||
#program_export
|
||||
WallMilliseconds :: () -> s64 { return WallMillisecondsHost() }
|
||||
@@ -0,0 +1,44 @@
|
||||
// Files for a portable bundle, read and written by the program that runs it,
|
||||
// such as ZX Runner, with the same reach as a native program.
|
||||
host_api :: #system_library "host_api";
|
||||
|
||||
// The size in bytes, -1 when nothing is there, -2 for a directory.
|
||||
FileStatusHost :: (path: string) -> s64 #foreign host_api;
|
||||
FileReadHost :: (path: string) -> string #foreign host_api;
|
||||
FileWriteHost :: (path: string, data: string) -> bool #foreign host_api;
|
||||
FileRemoveHost :: (path: string) -> bool #foreign host_api;
|
||||
FileRenameHost :: (source: string, destination: string) -> bool #foreign host_api;
|
||||
DirectoryMakeHost :: (path: string) -> bool #foreign host_api;
|
||||
// Entry names separated by "\n", without "." and "..".
|
||||
DirectoryListHost :: (path: string) -> string #foreign host_api;
|
||||
|
||||
FILE_MISSING :: -1;
|
||||
FILE_DIRECTORY :: -2;
|
||||
|
||||
#program_export
|
||||
FileStatus :: (path: string) -> s64 { return FileStatusHost(path) }
|
||||
|
||||
// The whole file, or ok false when it is missing, a directory or unreadable.
|
||||
#program_export
|
||||
FileRead :: (path: string) -> (data: string, ok: bool) {
|
||||
if FileStatusHost(path) < 0 { return "", false }
|
||||
return FileReadHost(path), true
|
||||
}
|
||||
|
||||
// Replaces the file with data; false when it could not be written.
|
||||
#program_export
|
||||
FileWrite :: (path: string, data: string) -> bool { return FileWriteHost(path, data) }
|
||||
|
||||
#program_export
|
||||
FileRemove :: (path: string) -> bool { return FileRemoveHost(path) }
|
||||
|
||||
#program_export
|
||||
FileRename :: (source: string, destination: string) -> bool {
|
||||
return FileRenameHost(source, destination)
|
||||
}
|
||||
|
||||
#program_export
|
||||
DirectoryMake :: (path: string) -> bool { return DirectoryMakeHost(path) }
|
||||
|
||||
#program_export
|
||||
DirectoryList :: (path: string) -> string { return DirectoryListHost(path) }
|
||||
@@ -0,0 +1,18 @@
|
||||
// The arguments and environment a runner passes a portable bundle. Argument 0
|
||||
// is the bundle's path; `zx app.zx a b` gives the bundle three arguments.
|
||||
host_api :: #system_library "host_api";
|
||||
|
||||
ArgumentCountHost :: () -> s32 #foreign host_api;
|
||||
ArgumentHost :: (index: s32) -> string #foreign host_api;
|
||||
EnvironmentHost :: (name: string) -> string #foreign host_api;
|
||||
|
||||
#program_export
|
||||
ArgumentCount :: () -> s32 { return ArgumentCountHost() }
|
||||
|
||||
// The argument at index, or "" outside 0..ArgumentCount() - 1.
|
||||
#program_export
|
||||
Argument :: (index: s32) -> string { return ArgumentHost(index) }
|
||||
|
||||
// The variable's value, or "" when it is unset.
|
||||
#program_export
|
||||
Environment :: (name: string) -> string { return EnvironmentHost(name) }
|
||||
@@ -57,7 +57,17 @@ CheckZxRunner :: (test: *NativeTest, share: string, data: string) -> bool {
|
||||
unused NativeRun(test, gap_build[:])
|
||||
gap := string.[runner, window]
|
||||
missing := NativeRun(test, gap[:], "", 1)
|
||||
return NativeAssert(test, !test.failed && TextContains(missing.error, "window_gap.zx needs platform capabilities"), "zx did not report a capability it lacks")
|
||||
if !NativeAssert(test, !test.failed && TextContains(missing.error, "window_gap.zx needs capabilities this ZX Runner does not provide: window_gap:OpenWindowHost"), "zx did not name a capability it lacks") { return false }
|
||||
// The base capabilities: arguments, files and the bundle's own assets.
|
||||
assets := CheckPath(test.work, "zx-assets")
|
||||
if !CreateDirectory(assets) || !NativeWrite(test, CheckPath(assets, "greeting.txt"), "from the bundle\n") { return false }
|
||||
base_source := CheckPath(test.work, "base_zx.zi")
|
||||
if !NativeWrite(test, base_source, "Process :: #import \"std/guest_process\";\nFiles :: #import \"std/guest_files\";\nAssets :: #import \"std/guest_assets\";\nmain :: () -> s32 {\n path := Process.Argument(1)\n greeting, found := Assets.AssetFind(\"zx-assets/greeting.txt\")\n if !found || !Files.FileWrite(path, greeting) { return 2 }\n copy, ok := Files.FileRead(path)\n print(\"% % %\", Process.ArgumentCount(), ok, copy)\n return 0\n}\n") { return false }
|
||||
base := CheckPath(test.work, "base_zx.zx")
|
||||
base_build := string.[test.ziran, "bundle", "--root", test.work, "--asset-dir", CheckConcat("zx-assets=", assets), "--entry", "base_zx:main", "-o", base, base_source]
|
||||
unused NativeRun(test, base_build[:])
|
||||
copied := string.[runner, base, CheckPath(test.work, "base-copy.txt")]
|
||||
return NativeExpect(test, copied[:], "2 true from the bundle\n")
|
||||
}
|
||||
|
||||
#program_export
|
||||
|
||||
Reference in new issue
Block a user