diff --git a/.gitattributes b/.gitattributes index 39bde7ffb3..02da392a22 100644 --- a/.gitattributes +++ b/.gitattributes @@ -101,4 +101,6 @@ Cargo.lock text .gitattributes export-ignore .gitignore export-ignore -.gitkeep export-ignore \ No newline at end of file +.gitkeep export-ignore +# Frozen byte manifests use LF on every platform. +platform/tests/fixtures/font-assets-*-v1.txt text eol=lf diff --git a/.github/workflows/windows-renderer.yml b/.github/workflows/windows-renderer.yml new file mode 100644 index 0000000000..1cc29c3cd6 --- /dev/null +++ b/.github/workflows/windows-renderer.yml @@ -0,0 +1,97 @@ +name: Windows D3D11 renderer +on: + push: + branches: [windows-warp-fallback] + pull_request: + paths: + - 'platform/src/os/windows/d3d11.rs' + - '.github/workflows/windows-renderer.yml' + workflow_dispatch: +permissions: + contents: read +jobs: + native-windows: + runs-on: windows-2025 + timeout-minutes: 45 + defaults: + run: + working-directory: makepad + steps: + - uses: actions/checkout@v4 + with: + persist-credentials: false + path: makepad + - uses: actions/checkout@v4 + with: + repository: OctoSense-org/Octoscript-Makepad + ref: 6cf2d90aa33ff4ae603f49c7557f3ecf986207f0 + path: octoscript-makepad + persist-credentials: false + - run: rustup toolchain install 1.98.0 --profile minimal + - uses: Swatinem/rust-cache@v2 + with: + workspaces: makepad -> target + - name: Check the native Windows platform + env: + RUSTUP_TOOLCHAIN: 1.98.0 + run: cargo check -p makepad-platform --all-targets + - name: Run existing native platform tests + env: + RUSTUP_TOOLCHAIN: 1.98.0 + run: cargo test -p makepad-platform --lib + - name: Build existing native example in release mode + env: + RUSTUP_TOOLCHAIN: 1.98.0 + run: cargo build --release -p makepad-example-hello-world + - name: Exercise the existing example on native Windows WARP + shell: pwsh + timeout-minutes: 5 + env: + MAKEPAD_D3D11_WARP: '1' + run: | + $ErrorActionPreference='Stop' + $sample=Join-Path $PWD 'local/agent_state/windows-warp-native' + New-Item -ItemType Directory -Path $sample -Force | Out-Null + $owned=Start-Process -FilePath (Join-Path $PWD 'target/release/makepad-example-hello-world.exe') -ArgumentList '--remote=8680' -WorkingDirectory $PWD -WindowStyle Normal -RedirectStandardOutput (Join-Path $sample 'stdout.log') -RedirectStandardError (Join-Path $sample 'stderr.log') -PassThru + try { + $ready=$false + for($attempt=0;$attempt -lt 45;$attempt++){ + if($owned.HasExited){throw 'Owned example exited during startup'} + try { + $state=Invoke-RestMethod 'http://127.0.0.1:8680/s' -TimeoutSec 2 + if($state.w.Count -gt 0){$ready=$true;break} + } catch {} + Start-Sleep -Milliseconds 500 + } + if(!$ready){throw 'Native example remote endpoint did not become ready'} + $tree=Invoke-RestMethod 'http://127.0.0.1:8680/snap?all=1' -TimeoutSec 10 + if(!($tree.s | Where-Object {$_.t -eq 'Hello GIF'})){throw 'Existing example widget tree was not rendered'} + $frame=Join-Path $sample 'native-warp.png' + Add-Type -AssemblyName System.Drawing + $drawn=$false + for($capture=0;$capture -lt 4;$capture++){ + $candidate=Join-Path $sample ("native-warp-"+$capture+".png") + Invoke-WebRequest 'http://127.0.0.1:8680/g?raw=1' -OutFile $candidate -TimeoutSec 30 + $image=[Drawing.Bitmap]::new($candidate) + try { + if($image.Width -lt 200 -or $image.Height -lt 150){throw 'Native frame dimensions invalid'} + $colors=[Collections.Generic.HashSet[int]]::new() + for($y=0;$y -lt $image.Height;$y+=8){for($x=0;$x -lt $image.Width;$x+=8){[void]$colors.Add($image.GetPixel($x,$y).ToArgb())}} + Write-Output ('Native WARP frame '+$capture+': '+$image.Width+'x'+$image.Height+', sampled colors='+$colors.Count) + if($colors.Count -ge 8){Copy-Item -LiteralPath $candidate -Destination $frame;$drawn=$true;break} + } finally {$image.Dispose()} + Start-Sleep -Seconds 1 + } + if(!$drawn){throw 'Native frame stayed blank through bounded startup checks'} + $observed=(Get-Content (Join-Path $sample 'stdout.log') -Raw)+(Get-Content (Join-Path $sample 'stderr.log') -Raw) + if($observed -notmatch 'using Windows WARP software rendering'){throw 'Native WARP path was not observed'} + Get-FileHash -LiteralPath $frame -Algorithm SHA256 | Format-List + } finally { + if(!$owned.HasExited){try {Invoke-WebRequest 'http://127.0.0.1:8680/quit' -TimeoutSec 10 | Out-Null} catch {};if(!$owned.WaitForExit(20000)){Stop-Process -Id $owned.Id;throw 'Owned example did not exit gracefully'}} + } + - uses: actions/upload-artifact@v4 + if: always() + with: + name: windows-warp-native-frame + path: makepad/local/agent_state/windows-warp-native/ + if-no-files-found: error diff --git a/draw/src/text/system_fonts.rs b/draw/src/text/system_fonts.rs index 03282816bc..82306c7453 100644 --- a/draw/src/text/system_fonts.rs +++ b/draw/src/text/system_fonts.rs @@ -68,6 +68,9 @@ pub fn font_dirs() -> Vec { } } } + } else if cfg!(any(target_os = "android", target_env = "ohos")) { + // Android and OpenHarmony/HarmonyOS keep the system fonts here. + dirs.push(PathBuf::from("/system/fonts")); } else if cfg!(windows) { if let Some(windir) = std::env::var_os("WINDIR") { dirs.push(PathBuf::from(windir).join("Fonts")); diff --git a/libs/stitch/src/exec.rs b/libs/stitch/src/exec.rs index 597aef24f9..e088ed2fee 100644 --- a/libs/stitch/src/exec.rs +++ b/libs/stitch/src/exec.rs @@ -8,7 +8,7 @@ use { error::Error, extern_::UnguardedExtern, extern_ref::UnguardedExternRef, - func::{Func, FuncEntity, UnguardedFunc}, + func::{Func, FuncEntity, HostFuncEntity, UnguardedFunc}, func_ref::UnguardedFuncRef, global::UnguardedGlobal, mem::UnguardedMem, @@ -40,9 +40,9 @@ use { /// /// On 64-bit Windows platforms, the "C" ABI corresponds to the "win64" ABI. This ABI allows only /// the first 4 arguments to be passed in hardware registers, regardless of their type. This is -/// insufficient for our needs, so on Windows platforms, we use the "sysv64" ABI instead. +/// insufficient for our needs, so on x86-64 Windows, we use the "sysv64" ABI instead. -#[cfg(not(windows))] +#[cfg(not(all(windows, target_arch = "x86_64")))] pub(crate) type ThreadedInstr = unsafe extern "C" fn( ip: Ip, sp: Sp, @@ -54,7 +54,7 @@ pub(crate) type ThreadedInstr = unsafe extern "C" fn( cx: Cx, ) -> ControlFlowBits; -#[cfg(windows)] +#[cfg(all(windows, target_arch = "x86_64"))] pub(crate) type ThreadedInstr = unsafe extern "sysv64" fn( ip: Ip, sp: Sp, @@ -289,7 +289,7 @@ pub(crate) fn exec( // Helper macros /// A helper macro for defining a `ThreadedInstr` with the correct ABI. -#[cfg(windows)] +#[cfg(all(windows, target_arch = "x86_64"))] macro_rules! threaded_instr { ($name:ident( $ip:ident: Ip, @@ -313,7 +313,7 @@ macro_rules! threaded_instr { ) -> ControlFlowBits $body }; } -#[cfg(not(windows))] +#[cfg(not(all(windows, target_arch = "x86_64")))] macro_rules! threaded_instr { ($name:ident( $ip:ident: Ip, @@ -561,6 +561,30 @@ threaded_instr!(call( next_instr(ip, sp, md, ms, ix, sx, dx, cx) }); +/// Calls a host function on the shared stack, returning `false` (with the +/// error stored in `cx`) if it failed. +/// +/// This lives out of line on purpose: the `Result` returned by the +/// trampoline is written through a pointer to a stack slot. If that slot +/// were in a handler's frame, its address would escape, LLVM could no longer +/// turn the handler's call to the next instruction into a sibling call, and +/// every host call would leave a frame behind on the native stack. +#[inline(never)] +unsafe fn call_host_func(func: &HostFuncEntity, sp: Sp, offset: usize, cx: Cx) -> bool { + let mut stack = (*cx).stack.take().unwrap_unchecked(); + stack.set_ptr(sp.cast::().add(offset).cast()); + match func.trampoline().clone().call((*cx).store, stack) { + Ok(stack) => { + (*cx).stack = Some(stack); + true + } + Err(error) => { + (*cx).error = Some(error); + false + } + } +} + threaded_instr!(call_host( ip: Ip, sp: Sp, @@ -576,20 +600,12 @@ threaded_instr!(call_host( let (offset, ip) = read_imm(ip); let (mem, ip): (Option, _) = read_imm(ip); - let mut stack = (*cx).stack.take().unwrap_unchecked(); - stack.set_ptr(sp.cast::().add(offset).cast()); let FuncEntity::Host(func) = func.as_ref() else { hint::unreachable_unchecked(); }; - let stack = match func.trampoline().clone().call((*cx).store, stack) { - Ok(stack) => stack, - Err(error) => { - (*cx).error = Some(error); - return ControlFlow::Error.to_bits(); - } - }; - - (*cx).stack = Some(stack); + if !call_host_func(func, sp, offset, cx) { + return ControlFlow::Error.to_bits(); + } let md; let ms; @@ -659,16 +675,9 @@ threaded_instr!(call_indirect( next_instr(ip, sp, md, ms, ix, sx, dx, cx) } FuncEntity::Host(func) => { - let mut stack = (*cx).stack.take().unwrap_unchecked(); - stack.set_ptr(sp.cast::().add(stack_offset).cast()); - let stack = match func.trampoline().clone().call((*cx).store, stack) { - Ok(stack) => stack, - Err(error) => { - (*cx).error = Some(error); - return ControlFlow::Error.to_bits(); - } - }; - (*cx).stack = Some(stack); + if !call_host_func(func, sp, stack_offset, cx) { + return ControlFlow::Error.to_bits(); + } let md; let ms; diff --git a/libs/stitch/tests/host_calls.rs b/libs/stitch/tests/host_calls.rs new file mode 100644 index 0000000000..12f3e32d7e --- /dev/null +++ b/libs/stitch/tests/host_calls.rs @@ -0,0 +1,75 @@ +//! A host function called in a loop must not grow the native stack. +//! +//! Every threaded instruction ends in a sibling call to the next one. A +//! handler that keeps something alive in its frame across that call leaves +//! the frame behind, so a module that calls a host function many times in one +//! invocation runs out of native stack. + +use makepad_stitch::{Engine, Func, Linker, Module, Store, Val}; + +const CALLS: i32 = 1_000_000; + +const WAT: &str = r#" +(module + (type $t (func (param i32) (result i32))) + (import "env" "tick" (func $tick (type $t))) + (table 1 funcref) + (elem (i32.const 0) $tick) + (func (export "direct") (param $n i32) (result i32) + (local $i i32) (local $acc i32) + (block $done + (loop $next + (br_if $done (i32.ge_u (local.get $i) (local.get $n))) + (local.set $acc (call $tick (local.get $acc))) + (local.set $i (i32.add (local.get $i) (i32.const 1))) + (br $next))) + (local.get $acc)) + (func (export "indirect") (param $n i32) (result i32) + (local $i i32) (local $acc i32) + (block $done + (loop $next + (br_if $done (i32.ge_u (local.get $i) (local.get $n))) + (local.set $acc (call_indirect (type $t) (local.get $acc) (i32.const 0))) + (local.set $i (i32.add (local.get $i) (i32.const 1))) + (br $next))) + (local.get $acc))) +"#; + +/// Calls `export(CALLS)` on a thread with a 1 MiB stack: a leak of even a +/// few hundred bytes per host call overflows it long before the loop ends. +fn run(export: &'static str) -> i32 { + std::thread::Builder::new() + .stack_size(1 << 20) + .spawn(move || { + let buf = wast::parser::ParseBuffer::new(WAT).unwrap(); + let mut wat: wast::Wat = wast::parser::parse(&buf).unwrap(); + let bytes = wat.encode().unwrap(); + let engine = Engine::new(); + let mut store = Store::new(engine); + let module = Module::new(store.engine(), &bytes).unwrap(); + let mut linker = Linker::new(); + let tick = Func::wrap(&mut store, |x: i32| -> i32 { x + 1 }); + linker.define("env", "tick", tick); + let instance = linker.instantiate(&mut store, &module).unwrap(); + let mut results = [Val::I32(0)]; + instance + .exported_func(export) + .unwrap() + .call(&mut store, &[Val::I32(CALLS)], &mut results) + .unwrap(); + results[0].to_i32().unwrap() + }) + .unwrap() + .join() + .unwrap() +} + +#[test] +fn host_calls_in_a_loop_keep_the_native_stack_flat() { + assert_eq!(run("direct"), CALLS); +} + +#[test] +fn indirect_host_calls_in_a_loop_keep_the_native_stack_flat() { + assert_eq!(run("indirect"), CALLS); +} diff --git a/libs/windows/windows-rs/src/Windows/mod.rs b/libs/windows/windows-rs/src/Windows/mod.rs index cd140a65b9..4301bdb633 100644 --- a/libs/windows/windows-rs/src/Windows/mod.rs +++ b/libs/windows/windows-rs/src/Windows/mod.rs @@ -107413,6 +107413,7 @@ pub const D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST: D3D_PRIMITIVE_TOPOLOGY = D3D_PR #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] pub struct D3D_DRIVER_TYPE(pub i32); pub const D3D_DRIVER_TYPE_UNKNOWN: D3D_DRIVER_TYPE = D3D_DRIVER_TYPE(0i32); +pub const D3D_DRIVER_TYPE_WARP: D3D_DRIVER_TYPE = D3D_DRIVER_TYPE(5i32); #[repr(transparent)] #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] pub struct D3D_FEATURE_LEVEL(pub i32); diff --git a/platform/build.rs b/platform/build.rs index f71651369f..c7d8e652b4 100644 --- a/platform/build.rs +++ b/platform/build.rs @@ -181,6 +181,8 @@ fn main() { // OpenHarmony's sysroot has no xkbcommon; its keyboard comes through ArkUI. if std::env::var("CARGO_CFG_TARGET_ENV").as_deref() != Ok("ohos") { println!("cargo:rustc-link-lib=xkbcommon"); + } else { + build_ohos_node_shim(&target, &out_dir); } } "android" => { @@ -189,3 +191,48 @@ fn main() { _ => (), } } + +/// OpenHarmony: compile `node_shim.cpp` (makepad's ArkUI node, created with +/// the NDK) and `web_shim.cpp` (system browsers through ArkWeb's NDK) with the +/// SDK's clang++, which cargo-makepad exports as +/// `CXX_` (and `AR_`), into a static library linked here. +/// No `cc` build dependency: one compiler call and one archive. +fn build_ohos_node_shim(target: &str, out_dir: &str) { + let srcs = ["src/os/linux/open_harmony/node_shim.cpp", "src/os/linux/open_harmony/web_shim.cpp"]; + for src in srcs { + println!("cargo:rerun-if-changed={src}"); + } + let key = target.replace('-', "_"); + println!("cargo:rerun-if-env-changed=CXX_{key}"); + println!("cargo:rerun-if-env-changed=AR_{key}"); + let (Ok(cxx), Ok(ar)) = (env::var(format!("CXX_{key}")), env::var(format!("AR_{key}"))) else { + println!("cargo:warning=CXX_{key}/AR_{key} not set: building without the OpenHarmony node shim"); + return; + }; + let lib = Path::new(out_dir).join("libmakepad_ohos_node_shim.a"); + let mut objs = Vec::new(); + for src in srcs { + let obj = Path::new(out_dir).join(Path::new(src).file_stem().unwrap()).with_extension("o"); + let status = std::process::Command::new(&cxx) + // No C++ runtime is linked: no exceptions, RTTI or guarded statics. + .args(["-c", "-fPIC", "-O2", "-std=c++17", "-fno-exceptions", "-fno-rtti", "-fno-threadsafe-statics", src, "-o"]) + .arg(&obj) + .status() + .expect("run the OpenHarmony clang++"); + assert!(status.success(), "compiling {src} failed"); + objs.push(obj); + } + let _ = std::fs::remove_file(&lib); + let status = std::process::Command::new(&ar) + .arg("crs") + .arg(&lib) + .args(&objs) + .status() + .expect("run llvm-ar"); + assert!(status.success(), "archiving the OpenHarmony shims failed"); + println!("cargo:rustc-link-search=native={out_dir}"); + println!("cargo:rustc-link-lib=static=makepad_ohos_node_shim"); + println!("cargo:rustc-link-lib=ace_ndk.z"); + println!("cargo:rustc-link-lib=ace_napi.z"); + println!("cargo:rustc-link-lib=ohweb"); +} diff --git a/platform/network/src/types.rs b/platform/network/src/types.rs index 03805d4a45..5a1a4715a3 100644 --- a/platform/network/src/types.rs +++ b/platform/network/src/types.rs @@ -66,6 +66,9 @@ pub struct HttpRequest { pub method: HttpMethod, #[cfg_attr(feature = "script", live)] pub headers: BTreeMap>, + /// Skip TLS certificate verification for this request. Intended for + /// loopback/dev endpoints with self-signed certificates; the per-isolate + /// host allowlist still applies, so this cannot widen an app's reach. #[cfg_attr(feature = "script", live)] pub ignore_ssl_cert: bool, #[cfg_attr(feature = "script", live)] diff --git a/platform/script/src/lib.rs b/platform/script/src/lib.rs index 69d10c0479..0313753cea 100644 --- a/platform/script/src/lib.rs +++ b/platform/script/src/lib.rs @@ -62,6 +62,7 @@ pub mod opcodes_vars; pub mod pod; pub mod pod_heap; pub mod prims; +pub mod query; pub mod shader; pub mod shader_backend; pub mod shader_builtins; diff --git a/platform/script/src/parser.rs b/platform/script/src/parser.rs index 486522200e..335a293e28 100644 --- a/platform/script/src/parser.rs +++ b/platform/script/src/parser.rs @@ -1387,6 +1387,25 @@ impl ScriptParser { } } + /// Close one branch of an `if`/`elif`/`else` (or a `match` arm) and hand + /// the join exactly one value: the statement-end `set_pop_to_me` that + /// follows lands its pop on the join (a standalone POP_TO_ME at the jump + /// target) or just past it, and that pop always consumes one value. An + /// arm whose tail leaves a value (assignment, call, bare value, nested + /// control flow) keeps it by cancelling the fused commit; an arm whose + /// tail hands nothing — `for`/`while` (FOR_END), `let`, `break`/ + /// `continue`, an empty body, or a tail whose commit already ran inside + /// the arm (a `;` tail) — must stand in a NIL. Without it the join's pop + /// reads through the base of the stack and traps with `pop_stack_value + /// on empty stack` (the #492 family). + fn close_branch(&mut self, last_was_sep: bool) { + if !last_was_sep && self.has_pop_to_me() { + self.clear_pop_to_me(); + } else { + self.push_code_none(NIL); + } + } + fn set_opcode_args(&mut self, index: u32, args: OpcodeArgs) { self.opcodes[index as usize].set_opcode_args(args); } @@ -1635,9 +1654,7 @@ impl ScriptParser { } => { // Block body done, expect } if tok.is_close_curly() { - if !last_was_sep && self.has_pop_to_me() { - self.clear_pop_to_me(); - } + self.close_branch(last_was_sep); self.state.push(State::MatchMaybeArm { temp_id, if_start, @@ -1699,9 +1716,7 @@ impl ScriptParser { } => { // Wildcard block body done, expect } if tok.is_close_curly() { - if !last_was_sep && self.has_pop_to_me() { - self.clear_pop_to_me(); - } + self.close_branch(last_was_sep); self.state.push(State::MatchWildcardEnd { prev_else_start, index, @@ -3583,6 +3598,13 @@ impl ScriptParser { if tok.is_close_curly() { if !last_was_sep && self.has_pop_to_me() { self.clear_pop_to_me(); + } else if !self.has_pop_to_me() { + // The protected tail hands no value (for/while/let/ + // empty body): stand in a NIL so the try join's pop + // has its value (#492 family). A value-ful tail + // terminated by `;` keeps its fused commit — the + // canonical-catch path rewrites that marker below. + self.push_code_none(NIL); } self.set_opcode_args( try_start, @@ -3675,6 +3697,11 @@ impl ScriptParser { // the try value even when a separator terminated it. if (canonical_catch || !last_was_sep) && self.has_pop_to_me() { self.clear_pop_to_me(); + } else if !self.has_pop_to_me() { + // A value-less catch tail (for/while/let/empty body) + // must still hand one value to the join — same + // contract as the protected block (#492 family). + self.push_code_none(NIL); } self.last_jump_target = self.code_len(); self.set_opcode_args( @@ -3795,9 +3822,7 @@ impl ScriptParser { last_was_sep, } => { if tok.is_close_curly() { - if !last_was_sep && self.has_pop_to_me() { - self.clear_pop_to_me(); - } + self.close_branch(last_was_sep); self.state.push(State::IfMaybeElse { if_start, was_block: true, @@ -3881,9 +3906,7 @@ impl ScriptParser { last_was_sep, } => { if tok.is_close_curly() { - if !last_was_sep && self.has_pop_to_me() { - self.clear_pop_to_me(); - } + self.close_branch(last_was_sep); self.last_jump_target = self.code_len(); self.set_opcode_args( else_start, diff --git a/platform/script/src/query.rs b/platform/script/src/query.rs new file mode 100644 index 0000000000..162746b83b --- /dev/null +++ b/platform/script/src/query.rs @@ -0,0 +1,114 @@ +//! The VM query API: two read-only functions an editor-facing consumer uses to +//! ask which names exist. +//! +//! ADR 0001 (App Flow) fixes the count and the semantics before the first +//! caller exists, because every addition to a shared crate's public surface is +//! a promise someone has to keep for as long as the crate is at that major +//! version — and this crate is at `1.0.0` with no CI and no crates.io release +//! to keep the promise for it. The two functions: +//! +//! 1. [`object_keys`] — enumerate an object's own keys: each name once, in a +//! deterministic (sorted) order, without values. Not the prototype chain. +//! 2. [`module_object`] — get a module object by name; a name the VM does not +//! have is `None`. +//! +//! Both are reads an editor performs per keystroke: neither mutates the heap, +//! runs script, charges an instruction budget, or traps. A query API that +//! inherited the default type getter's trap would turn a completion request +//! into a script error, and the one place a language server must not produce an +//! error is the path it takes while the person is typing. +//! +//! ## The stability promise +//! +//! Within `1.x` this surface is **additive only**: new functions may appear; +//! removing or reinterpreting either function, or changing the order +//! [`object_keys`] returns, is `2.0` and must be called out in the fork's +//! release notes. Because there is no CI and no crates.io, the promise is kept +//! by hand and gated by two things that run with one command anyone can type: +//! +//! * `tests/query_api_surface.rs` — the checked-in surface file stops the build +//! if either function is removed, renamed or reshaped, so a breaking change +//! cannot land silently. +//! * `tests/query_corpus.txt` with `tests/query_api_corpus.rs` — the fixture +//! corpus of the real objects a VM installs, with the key sets each must +//! answer. A change to a module's members is a visible diff in a checked-in +//! file, which is the intended friction, and a pull request that changes +//! either function names that diff. +//! +//! What is deliberately NOT here: prototype-chain traversal (the consumer walks +//! it with [`crate::vm::ScriptVm::proto_map_iter_mut_with`] or the object's own +//! `proto` field — see ADR 0001 §2), and every question that reduces to neither +//! function. If a third function is ever needed, the ADR is amended, not +//! quietly extended. + +use crate::heap::*; +use crate::makepad_live_id::*; +use crate::suggest::key_to_string_opt; +use crate::value::*; + +/// Enumerate an object's own keys. +/// +/// The object's own entries — the ones its map and its vec hold — each name +/// once, sorted, without values. The prototype chain is not walked: a widget's +/// inherited properties are the consumer's business, reached with +/// [`crate::vm::ScriptVm::proto_map_iter_mut_with`] or +/// [`crate::heap::ScriptHeap::proto`]. +/// +/// The order is part of the contract: sorted, so it does not change when an +/// object grows past [`crate::value_map::ValueMap`]'s spill threshold, and a +/// golden fixture built on it does not flake for a reason no reader could see. +/// +/// An entry whose key is not a recoverable name — a number, say — is skipped: +/// this answers with names, and a name the VM cannot recover (an id no source +/// ever spelled, which the VM's own "Did you mean" lists also cannot show) is +/// not invented here. +pub fn object_keys(heap: &ScriptHeap, object: ScriptObject) -> Vec { + let mut names = Vec::new(); + for kv in heap.vec_ref(object) { + if let Some(name) = key_to_string_opt(heap, kv.key) { + names.push(name); + } + } + for (key, _) in heap.map_ref(object).iter() { + if let Some(name) = key_to_string_opt(heap, *key) { + names.push(name); + } + } + names.sort(); + names.dedup(); + names +} + +/// Get a module object by name. +/// +/// A name is a dotted path of keys rooted at the VM's module table — the same +/// names a script writes after `use`, with or without the leading `mod` +/// (`mod` is the table's own name, the way the global `mod` is the table): +/// `"sys"`, `"mod.sys"`, `"mod.prelude.widgets"`. +/// +/// A name the VM does not have — an unknown module, a missing key along the +/// path, a segment that is not an object — is `None`. It is never a trap and +/// never a panic: the VM has no business deciding what an unknown name means, +/// and a completion request is not the place to find out. +pub fn module_object(heap: &ScriptHeap, name: &str) -> Option { + let mut segments = name.split('.'); + // The module table is its own `mod`: a leading one starts at the table, + // any other first segment is a module inside it. + let mut current = heap.modules; + if let Some(first) = segments.next() { + if first != "mod" { + current = segment_object(heap, current, first)?; + } + } + for segment in segments { + current = segment_object(heap, current, segment)?; + } + Some(current) +} + +/// One segment of a dotted name: the object stored under that key, if any. +fn segment_object(heap: &ScriptHeap, object: ScriptObject, segment: &str) -> Option { + heap.map_ref(object) + .get(&LiveId::from_str(segment).into()) + .and_then(|entry| entry.value.as_object()) +} diff --git a/platform/script/src/suggest.rs b/platform/script/src/suggest.rs index 1f2cb21999..18c5a4ad51 100644 --- a/platform/script/src/suggest.rs +++ b/platform/script/src/suggest.rs @@ -625,7 +625,7 @@ fn key_to_string(heap: &ScriptHeap, key: ScriptValue) -> String { } /// Try to convert a ScriptValue key to a string, returns None if not possible -fn key_to_string_opt(heap: &ScriptHeap, key: ScriptValue) -> Option { +pub(crate) fn key_to_string_opt(heap: &ScriptHeap, key: ScriptValue) -> Option { if let Some(id) = key.as_id() { return id.as_string(|s| s.map(|s| s.to_string())); } diff --git a/platform/script/std/tests/headless.rs b/platform/script/std/tests/headless.rs index 6e8d73e804..a61d4e0717 100644 --- a/platform/script/std/tests/headless.rs +++ b/platform/script/std/tests/headless.rs @@ -106,3 +106,98 @@ fn headless_http_request_resolves_promise_through_script_std() { _ => panic!("unexpected promise queue storage kind"), } } + +/// The script-side `net.HttpRequest{..}` constructor exposes `ignore_ssl_cert` +/// and the flag reaches the backend untouched — loopback/dev gateways with +/// self-signed certs depend on it. (Field presence was once mis-reported as +/// missing; this pins the round-trip so the question never needs re-asking.) +#[test] +fn headless_http_request_ignore_ssl_cert_roundtrip() { + struct CaptureBackend { + seen: std::sync::Mutex>, + } + + impl NetworkBackend for CaptureBackend { + fn http_start( + &self, + request_id: LiveId, + request: HttpRequest, + sink: EventSink, + ) -> Result<(), makepad_script_std::makepad_network::NetworkError> { + *self.seen.lock().unwrap() = Some(request.ignore_ssl_cert); + let response = + HttpResponse::new(LiveId(42), 200, BTreeMap::new(), Some(b"ok".to_vec())); + sink.emit(NetworkResponse::HttpResponse { + request_id, + response, + }) + } + + fn http_cancel( + &self, + _request_id: LiveId, + ) -> Result<(), makepad_script_std::makepad_network::NetworkError> { + Ok(()) + } + + fn ws_open( + &self, + _socket_id: LiveId, + _request: HttpRequest, + _sink: EventSink, + ) -> Result<(), makepad_script_std::makepad_network::NetworkError> { + Ok(()) + } + + fn ws_send( + &self, + _socket_id: LiveId, + _message: WsSend, + ) -> Result<(), makepad_script_std::makepad_network::NetworkError> { + Ok(()) + } + + fn ws_close( + &self, + _socket_id: LiveId, + ) -> Result<(), makepad_script_std::makepad_network::NetworkError> { + Ok(()) + } + } + + let backend = Arc::new(CaptureBackend { + seen: std::sync::Mutex::new(None), + }); + let runtime = Arc::new(NetworkRuntime::with_backend(backend.clone())); + let mut host = ScriptVmHost::new((), ScriptStd::with_network_runtime(runtime)); + host.script_vm = Some(Box::new(ScriptVmBase::new())); + + let promise = with_vm(&mut host, |vm| { + script_mod(vm); + vm.eval(script! { + use mod.std + use mod.net + let p = std.promise() + let req = net.HttpRequest{ + url: "https://127.0.0.1:8195/" + method: net.HttpMethod.GET + headers: {"User-Agent": "makepad-test/1.0"} + ignore_ssl_cert: true + } + net.http_request(req) do net.HttpEvents{ + on_response: |res| p.resolve(res.status_code) + on_error: |_err| p.resolve(-1) + } + p + }) + }); + assert!(!promise.is_err(), "script eval failed: {promise:?}"); + + let responses = pump_network_runtime(&mut host); + assert_eq!(responses.len(), 1); + assert_eq!( + *backend.seen.lock().unwrap(), + Some(true), + "ignore_ssl_cert did not reach the backend" + ); +} diff --git a/platform/script/tests/if_for_else_stack.rs b/platform/script/tests/if_for_else_stack.rs new file mode 100644 index 0000000000..f39edf8202 --- /dev/null +++ b/platform/script/tests/if_for_else_stack.rs @@ -0,0 +1,315 @@ +//! Regression tests for OctoSense-org/OctoSense#492: an +//! `if … { … for … } else { … }` whose true branch contains a `for` loop +//! left the script VM stack unbalanced; the next call in the same body +//! trapped with `pop_stack_value on empty stack`, and when the body was an +//! `on_render` handler the whole output was silently discarded (the view +//! rendered nothing). +//! +//! Root cause: the value-handoff contract of conditional jumps. Every arm of +//! an if/elif/else — and of try/catch and match, which desugar onto the same +//! IF_TEST/IF_ELSE joins — must hand the join exactly one value, because the +//! statement-end `set_pop_to_me` following the construct lands its pop on the +//! join (a standalone POP_TO_ME at the jump target) or just past it. Arms +//! whose tail produced no value — `for`/`while` (FOR_END), `let`, `break`/ +//! `continue`, an empty body, or a `;` tail whose commit already ran inside +//! the arm — handed nothing, so the join's pop read through the base of the +//! stack. The fix stands in a NIL for those arms (parser `close_branch` and +//! the try-block close paths). +//! +//! Most tests evaluate a helper `f()` and then call `next_call(f())`: the +//! crash used to surface on that *next* call, possibly far from the helper +//! that corrupted the stack. + +use makepad_script::*; + +fn test_vm() -> ScriptVm<'static> { + let host = Box::leak(Box::new(ScriptVmHost::new(0i32, ()))); + let mut vm = ScriptVm { + host, + bx: Box::new(ScriptVmBase::new()), + }; + vm.bx.captured_errors = Some(Vec::new()); + vm +} + +fn eval_str(vm: &mut ScriptVm, name: &str, code: &str) -> ScriptValue { + // Bounded: a regression must fail the test, not wedge the suite. + vm.with_instruction_limit(500_000, |vm| { + vm.eval(ScriptMod { + cargo_manifest_path: String::new(), + module_path: String::new(), + file: format!("if_for_else_stack_{name}"), + line: 0, + column: 0, + code: code.to_string(), + values: vec![], + }) + }) +} + +/// Run `body` as the body of `f()`, then call `next_call(f())` — the shape +/// the bug surfaced in. Returns `f() + 1`; asserts a clean run (no captured +/// errors) and a numeric result. +fn run_body(name: &str, body: &str) -> f64 { + let vm = &mut test_vm(); + let code = + format!("fn f(){{\n{body}\n}}\nfn next_call(x){{ x + 1 }}\nlet a = f()\nnext_call(a)"); + let v = eval_str(vm, name, &code); + let errors = vm.take_errors(); + assert!(errors.is_empty(), "{name}: unexpected errors: {errors:?}"); + v.as_number() + .unwrap_or_else(|| panic!("{name}: expected a number, got {v:?}")) +} + +// --------------------------------------------------------------------------- +// The exact shape from the issue +// --------------------------------------------------------------------------- + +/// The issue's minimal repro: the helper computes fine; the trap surfaced on +/// the next call in the caller. +#[test] +fn issue_492_exact_shape() { + let vm = &mut test_vm(); + let v = eval_str( + vm, + "issue492", + r#" +fn bad(){ + let total = 0 + if total >= 0 { + for j in 2 { total += j } + } else { total = 100 } + total +} +fn next_call(x){ x + 1 } +let a = bad() +next_call(a) +"#, + ); + let errors = vm.take_errors(); + assert!(errors.is_empty(), "unexpected errors: {errors:?}"); + assert_eq!(v.as_number(), Some(2.0), "got {v:?}"); +} + +/// Removing the `else` made the crash go away before the fix; both stay +/// working. +#[test] +fn without_else_still_works() { + assert_eq!( + run_body( + "no_else", + " let t = 0\n if t >= 0 { for j in 2 { t += j } }\n t" + ), + 2.0 + ); +} + +/// The untaken-true-branch variant (the else branch runs) stayed working. +#[test] +fn untaken_true_branch_runs_the_else() { + assert_eq!( + run_body( + "untaken", + " let t = 100\n if t >= 1000 { for j in 2 { t += j } } else { t = 100 }\n t" + ), + 101.0 + ); +} + +/// A statement after the loop inside the true branch kept the arm +/// value-ful, so this shape worked before the fix and must keep working. +#[test] +fn statement_after_the_loop_in_the_true_branch() { + assert_eq!( + run_body( + "after_loop", + " let t = 0\n if t >= 0 { for j in 2 { t += j }\n t += 10 } else { t = 100 }\n t" + ), + 12.0 + ); +} + +// --------------------------------------------------------------------------- +// The value-less-tail family: every shape used to trap (or, in expression +// contexts, drop the `let` binding); each hand-off now stands in a NIL +// --------------------------------------------------------------------------- + +#[test] +fn value_less_tails_hand_the_join_a_value() { + for (name, body, expect) in [ + ( + "for_tail", + " let t = 0\n if t >= 0 { for j in 2 { t += j } } else { t = 100 }\n t", + 2.0, + ), + ( + "while_tail", + " let t = 0\n if t >= 0 { while t < 2 { t += 1 } } else { t = 9 }\n t", + 3.0, + ), + ( + "let_tail", + " let t = 0\n if t >= 0 { let q = 1 } else { t = 2 }\n t", + 1.0, + ), + ( + "semicolon_tail", + " let t = 0\n if t >= 0 { t += 1; } else { t = 2 }\n t", + 2.0, + ), + ( + "empty_branch", + " let t = 0\n if t >= 0 { } else { t = 2 }\n t", + 1.0, + ), + ( + "nested_if_tail", + " let t = 0\n if t >= 0 { if t >= 0 { t += 1 } else { t = 2 } } else { t = 3 }\n t", + 2.0, + ), + ( + "both_branches_for", + " let t = 0\n if t >= 0 { for j in 2 { t += j } } else { for j in 2 { t += j } }\n t", + 2.0, + ), + ( + "no_else_mid_statement", + " let t = 0\n if t >= 0 { for j in 2 { t += j } }\n let q = 1\n t + q", + 3.0, + ), + ( + "no_else_semicolon_tail", + " let t = 0\n if t >= 0 { t += 1; }\n t", + 2.0, + ), + ( + "plain_tail_control", + " let t = 0\n if t >= 0 { t += 1 } else { t = 2 }\n t", + 2.0, + ), + ] { + assert_eq!(run_body(name, body), expect, "{name}"); + } +} + +// --------------------------------------------------------------------------- +// try/catch, match and elif desugar onto the same joins +// --------------------------------------------------------------------------- + +#[test] +fn try_match_and_elif_hand_the_join_a_value() { + for (name, body, expect) in [ + ( + "try_for_tail", + " let t = 0\n try { for j in 2 { t += j } } catch { t = 5 }\n t", + 2.0, + ), + ( + "try_for_then_statement", + " let t = 0\n try { for j in 2 { t += j } } catch { t = 5 }\n let q = 1\n t + q", + 3.0, + ), + ( + "try_semicolon_tail", + " let t = 0\n try { t += 1; } catch { t = 5 }\n t", + 2.0, + ), + ( + "catch_for_not_taken", + " let t = 0\n try { t += 1 } catch { for j in 2 { t += j } }\n t", + 2.0, + ), + ( + "catch_for_taken", + " let t = 0\n try { t = undefined_thing.x } catch { for j in 2 { t += j } }\n t", + 2.0, + ), + ( + "match_for_tail", + " let t = 0\n match 1 {\n 1 => { for j in 2 { t += j } }\n _ => { t = 5 }\n }\n t", + 2.0, + ), + ( + "match_semicolon_tail", + " let t = 0\n match 1 {\n 1 => { t += 1; }\n _ => { t = 5 }\n }\n t", + 2.0, + ), + ( + "match_single_arm_for", + " let t = 0\n match 1 {\n 1 => { for j in 2 { t += j } }\n }\n t", + 2.0, + ), + ( + "elif_for_tail", + " let t = 0\n if t >= 3 { t = 1 } elif t >= 0 { for j in 2 { t += j } } else { t = 5 }\n t", + 2.0, + ), + ] { + assert_eq!(run_body(name, body), expect, "{name}"); + } +} + +// --------------------------------------------------------------------------- +// Expression contexts: a value-less branch used to drop the `let` binding +// ("variable x not found" after the trap) +// --------------------------------------------------------------------------- + +#[test] +fn if_expression_assignment_with_value_less_branch() { + for (name, body, expect) in [ + ( + "expr_plain", + " let t = 0\n let x = if t >= 0 { t += 1 } else { 2 }\n (x) + 0", + 1.0, + ), + ( + "expr_for", + " let t = 0\n let x = if t >= 0 { for j in 2 { t += j } } else { 2 }\n (x) + 0", + 1.0, + ), + ( + "expr_empty", + " let t = 0\n let x = if t >= 0 { } else { 5 }\n (x) + 0", + 1.0, + ), + ] { + assert_eq!(run_body(name, body), expect, "{name}"); + } +} + +// --------------------------------------------------------------------------- +// The on_render emission contract stays intact for the fixed shapes +// --------------------------------------------------------------------------- + +/// The same if/for/else shape inside an object-literal body must still +/// commit its emitted child (in the issue this is the case that lost the +/// whole output silently). +#[test] +fn if_for_else_branch_emits_into_literal() { + let vm = &mut test_vm(); + let v = eval_str( + vm, + "emit", + r#"{ + let n = 0 + if n >= 0 { + for j in 2 { n += j } + {v: n} + } else { {v: 9} } +}"#, + ); + let errors = vm.take_errors(); + assert!(errors.is_empty(), "unexpected errors: {errors:?}"); + let obj = v.as_object().expect("expected an object result"); + let len = vm.bx.heap.iter_len(obj); + let mut vs = Vec::new(); + for i in 0..len { + let kv = vm.bx.heap.iter_key_value(obj, i, vm.trap()); + if let Some(child) = kv.value.as_object() { + if let Some(n) = vm.bx.heap.value(child, id!(v).into(), vm.trap()).as_number() { + vs.push(Some(n)); + } + } + } + assert_eq!(vs, vec![Some(1.0)], "emitted children: {vs:?}"); +} diff --git a/platform/script/tests/query_api.rs b/platform/script/tests/query_api.rs new file mode 100644 index 0000000000..ceabfae582 --- /dev/null +++ b/platform/script/tests/query_api.rs @@ -0,0 +1,227 @@ +//! The VM query API: `object_keys` and `module_object` — the two functions +//! ADR 0001 fixes and App Flow #9 ships. +//! +//! What these tests pin down, in the order the acceptance criteria name them: +//! +//! * `object_keys` answers "what names does this object have" — its own +//! entries, each name once, sorted, without values, and without walking the +//! prototype chain. +//! * `module_object` answers "give me the object called X" — a module object by +//! (dotted) name, and `None` for a name the VM does not have. +//! * Neither runs script, mutates the heap or traps: they are the reads an +//! editor performs per keystroke, and the one place a language server must +//! not produce an error is the path it takes while the person is typing. +//! * The prototype chain is NOT in the API, and IS walkable from outside with +//! the accessors that already exist — the last test is the proof ADR 0001 §2 +//! asks for, and the reason leaving the walk out is safe rather than tidy. + +use makepad_script::query; +use makepad_script::*; + +fn test_vm() -> ScriptVm<'static> { + let host = Box::leak(Box::new(ScriptVmHost::new((), ()))); + ScriptVm { + host, + bx: Box::new(ScriptVmBase::new()), + } +} + +fn eval_value(vm: &mut ScriptVm, code: &str) -> ScriptValue { + vm.with_instruction_limit(500_000, |vm| { + vm.eval(ScriptMod { + file: "query_api".into(), + code: code.into(), + ..Default::default() + }) + }) +} + +fn eval_object(vm: &mut ScriptVm, code: &str) -> ScriptObject { + eval_value(vm, code) + .as_object() + .unwrap_or_else(|| panic!("expected an object from {code:?}")) +} + +// ---------------------------------------------------------------- object_keys + +#[test] +fn own_keys_come_back_sorted_with_each_name_once() { + let mut vm = test_vm(); + let object = eval_object(&mut vm, "let o = {b: 1, a: 2, c: 3}\no"); + assert_eq!( + query::object_keys(&vm.bx.heap, object), + vec!["a", "b", "c"], + "own keys, each name once, in the deterministic (sorted) order" + ); +} + +#[test] +fn a_name_stored_as_a_string_key_is_a_key_too() { + let mut vm = test_vm(); + let object = eval_object(&mut vm, "let o = {\"a b\": 1, plain: 2}\no"); + assert_eq!( + query::object_keys(&vm.bx.heap, object), + vec!["a b", "plain"], + "a key is a name whether the VM holds it as an id or as a string" + ); +} + +#[test] +fn own_keys_do_not_walk_the_prototype_chain() { + let mut vm = test_vm(); + // `view{text: 3}` is a fresh object whose prototype is `view`: its own keys + // stop at `text`, exactly as ADR 0001 §2 fixes them. + let object = eval_object(&mut vm, "let view = {draw_bg: 1, cursor: 2}\nview{text: 3}"); + assert_eq!( + query::object_keys(&vm.bx.heap, object), + vec!["text"], + "inherited names are the consumer's business, not the VM's" + ); +} + +#[test] +fn a_key_that_is_not_a_name_is_not_returned() { + let mut vm = test_vm(); + // A numeric key has no name to return; it is skipped rather than invented. + let object = eval_object(&mut vm, "let o = {1: 2}\no"); + assert_eq!( + query::object_keys(&vm.bx.heap, object), + Vec::::new(), + "an entry with no recoverable name is not a key" + ); +} + +#[test] +fn querying_runs_no_script_and_reports_no_error() { + let mut vm = test_vm(); + let object = eval_object(&mut vm, "let o = {a: 1}\no"); + let module = query::module_object(&vm.bx.heap, "std").unwrap(); + let _ = query::object_keys(&vm.bx.heap, object); + let _ = query::object_keys(&vm.bx.heap, module); + let _ = query::module_object(&vm.bx.heap, "nope"); + assert!( + vm.take_errors().is_empty(), + "a query is a read: it evaluates nothing, so it cannot raise" + ); +} + +// -------------------------------------------------------------- module_object + +#[test] +fn a_module_the_vm_has_comes_back_by_name() { + let vm = test_vm(); + let std = query::module_object(&vm.bx.heap, "std").expect("mod.std is installed"); + assert!( + query::object_keys(&vm.bx.heap, std).contains(&"print".to_string()), + "the module object is the one the script sees: its keys are the stdlib's" + ); +} + +#[test] +fn the_leading_mod_is_the_module_table_itself() { + let vm = test_vm(); + assert_eq!( + query::module_object(&vm.bx.heap, "mod").map(|o| o.index()), + Some(vm.bx.heap.modules.index()), + "`mod` names the module table, the way it does in a script" + ); + assert_eq!( + query::module_object(&vm.bx.heap, "mod.std").map(|o| o.index()), + query::module_object(&vm.bx.heap, "std").map(|o| o.index()), + "with or without the leading `mod`, the same object answers" + ); +} + +#[test] +fn a_dotted_name_walks_keys_into_the_object() { + let mut vm = test_vm(); + eval_value(&mut vm, "mod.mytest = {inner: {a: 1}}\n1"); + let inner = query::module_object(&vm.bx.heap, "mod.mytest.inner") + .expect("a dotted name walks the module table's keys"); + assert_eq!(query::object_keys(&vm.bx.heap, inner), vec!["a"]); + assert!(query::module_object(&vm.bx.heap, "mod.mytest").is_some()); + assert!( + query::module_object(&vm.bx.heap, "mod.mytest.nope").is_none(), + "the walk stops at the first key that is missing or not an object" + ); + assert!( + query::module_object(&vm.bx.heap, "mod.nope.inner").is_none(), + "a missing first segment is nil, not a trap" + ); +} + +#[test] +fn module_object_returns_the_object_the_script_sees() { + let mut vm = test_vm(); + let assigned = eval_object(&mut vm, "mod.mytest = {a: 1}\nmod.mytest"); + assert_eq!( + query::module_object(&vm.bx.heap, "mod.mytest").map(|o| o.index()), + Some(assigned.index()), + "the object the name answers with is the object the script assigned" + ); +} + +#[test] +fn a_name_the_vm_does_not_have_is_none_never_a_trap() { + let mut vm = test_vm(); + for name in ["", "mod.nope", "nope", "nope.deeper", "mod.", "mod..std", "std.print.x"] { + assert!( + query::module_object(&vm.bx.heap, name).is_none(), + "{name:?} must be nil, not a panic" + ); + } + assert!(vm.take_errors().is_empty()); +} + +// ------------------------------------------------- the prototype chain, walked + +#[test] +fn the_prototype_chain_is_walkable_from_outside_with_the_existing_accessors() { + let mut vm = test_vm(); + // A widget's inherited properties live on the prototype: `draw_bg` comes + // from the view, `text` is the widget's own. The API does not walk the + // chain; the consumer does, with `heap.proto` (the object's own proto + // field) and `object_keys`. This test is ADR 0001 §2's fourth criterion. + let widget = eval_object(&mut vm, "let view = {draw_bg: 1, cursor: 2}\nview{text: 3}"); + assert_eq!(query::object_keys(&vm.bx.heap, widget), vec!["text"]); + + let mut names = Vec::new(); + let mut current = Some(widget); + while let Some(object) = current { + names.extend(query::object_keys(&vm.bx.heap, object)); + current = vm.bx.heap.proto(object).as_object(); + } + names.sort(); + names.dedup(); + assert_eq!( + names, + vec!["cursor", "draw_bg", "text"], + "the consumer reaches every inherited name with the accessors that already exist" + ); + + // The other accessor the ADR names walks the same chain root to leaf: the + // closure sees the root's map first and the object's own map last. + let mut map_lengths = Vec::new(); + vm.proto_map_iter_mut_with(widget, &mut |_vm, map| map_lengths.push(map.len())); + assert_eq!( + map_lengths, + vec![2, 1], + "proto_map_iter_mut_with visits root to leaf: the view's two keys, then the widget's one" + ); +} + +#[test] +fn a_shadowed_name_is_the_consumers_policy_not_the_vms() { + let mut vm = test_vm(); + // The widget declares its own draw_bg; the prototype still has one. The API + // reports both objects' own keys and leaves which one wins to the consumer + // — that is the policy ADR 0001 §2 keeps out of the VM. + let widget = eval_object(&mut vm, "let view = {draw_bg: 1}\nview{draw_bg: 2}"); + let mut seen = Vec::new(); + let mut current = Some(widget); + while let Some(object) = current { + seen.extend(query::object_keys(&vm.bx.heap, object)); + current = vm.bx.heap.proto(object).as_object(); + } + assert_eq!(seen, vec!["draw_bg", "draw_bg"], "both are reported; deduplicating is the consumer's call"); +} diff --git a/platform/script/tests/query_api_corpus.rs b/platform/script/tests/query_api_corpus.rs new file mode 100644 index 0000000000..3d8fd79b2f --- /dev/null +++ b/platform/script/tests/query_api_corpus.rs @@ -0,0 +1,158 @@ +//! The fixture corpus for the VM query API — ADR 0001 §4, App Flow #9. +//! +//! The corpus (`query_corpus.txt`, beside this file) names the modules a real +//! isolate installs and the key sets each must answer. This is the runner for +//! the entries tagged `script`: the modules `makepad-script` itself installs, +//! which a bare `ScriptVmBase` has. It runs with one command anyone can type — +//! `cargo test -p makepad-script` — because the fork has no CI and the crate is +//! not on crates.io, so a green local run that nobody wrote down is not a +//! promise. The widgets-tagged entries are checked by +//! `widgets/tests/vm_query_corpus.rs`, which reads this same file. +//! +//! What the runner proves, per entry: +//! +//! * the name answers with a module object (`module_object`), +//! * `object_keys` answers with exactly the corpus's key set, +//! * the object holds exactly the entry count the corpus records — a name is +//! not always recoverable (a key registered in Rust with `id!` and never +//! spelled by source is an id the VM cannot turn back into a string, and +//! `object_keys` does not invent one), so the count is what keeps an entry +//! the corpus cannot name a visible diff rather than a silent shrink, +//! * every entry here is a name — for these modules the crate interns its own +//! names, so the count and the key set must agree exactly, +//! * and the module table holds exactly the modules the corpus names for this +//! runner, so a module added or removed upstream is drift, not silence. + +use makepad_script::query; +use makepad_script::*; + +const CORPUS: &str = include_str!("query_corpus.txt"); +const RUNNER: &str = "script"; + +fn test_vm() -> ScriptVm<'static> { + let host = Box::leak(Box::new(ScriptVmHost::new((), ()))); + ScriptVm { + host, + bx: Box::new(ScriptVmBase::new()), + } +} + +struct Entry { + name: String, + runner: String, + keys: Vec, + total: usize, +} + +fn parse_corpus(text: &str) -> Vec { + text.lines() + .map(|line| line.trim()) + .filter(|line| !line.is_empty() && !line.starts_with('#')) + .map(|line| { + let mut fields = line.split('\t'); + let name = fields.next().unwrap_or_default().to_string(); + let runner = fields.next().unwrap_or_default().to_string(); + let keys = fields + .next() + .unwrap_or_default() + .split(',') + .filter(|key| !key.is_empty()) + .map(|key| key.to_string()) + .collect(); + let total = fields + .next() + .and_then(|total| total.parse().ok()) + .unwrap_or(0); + Entry { name, runner, keys, total } + }) + .collect() +} + +#[test] +fn the_corpus_matches_the_modules_this_vm_installs() { + let vm = test_vm(); + if std::env::var_os("QUERY_CORPUS_WRITE").is_some() { + regenerate(&vm, CORPUS); + return; + } + let entries: Vec = parse_corpus(CORPUS) + .into_iter() + .filter(|entry| entry.runner == RUNNER) + .collect(); + assert!( + !entries.is_empty(), + "the corpus names no {RUNNER} entries — it has been emptied, not updated" + ); + + for entry in &entries { + let module = query::module_object(&vm.bx.heap, &entry.name) + .unwrap_or_else(|| panic!("the corpus names {}, which this VM does not install", entry.name)); + let keys = query::object_keys(&vm.bx.heap, module); + let total = vm.bx.heap.iter_len(module); + assert_eq!( + keys, entry.keys, + "corpus drift for {}: object_keys answers with a different set \ + (if the module's members changed on purpose, regenerate with \ + QUERY_CORPUS_WRITE=1 and name the diff in the pull request)", + entry.name + ); + assert_eq!( + total, entry.total, + "corpus drift for {}: the object holds {} entries, not the {} the \ + corpus records (an entry the VM cannot name is still a diff)", + entry.name, total, entry.total + ); + assert_eq!( + keys.len(), + total, + "{} has {} entries but only {} are names: an entry the VM cannot \ + name would silently shrink the corpus", + entry.name, + total, + keys.len() + ); + } + + let installed = vm.bx.heap.iter_len(vm.bx.heap.modules); + assert_eq!( + installed, + entries.len(), + "the module table holds {installed} modules but the corpus names {} for \ + this runner: a module was added or removed without the corpus moving", + entries.len() + ); +} + +/// Rewrite this runner's entries from the VM, leaving every other line — the +/// header, the comments and the other runner's entries — exactly as it stands. +/// Run the test again afterwards: a regenerated corpus is a diff a person reads +/// and names in the pull request, not a green checkmark. +fn regenerate(vm: &ScriptVm, corpus: &str) { + let path = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/query_corpus.txt"); + let mut out = String::new(); + for line in corpus.lines() { + let trimmed = line.trim(); + let entry = parse_corpus(trimmed).into_iter().next(); + let rewritten = match entry { + Some(entry) if entry.runner == RUNNER => { + match query::module_object(&vm.bx.heap, &entry.name) { + Some(module) => format!( + "{}\t{}\t{}\t{}", + entry.name, + entry.runner, + query::object_keys(&vm.bx.heap, module).join(","), + vm.bx.heap.iter_len(module) + ), + // Not installed here any more: leave the line for the + // check to report, rather than silently dropping it. + None => line.to_string(), + } + } + _ => line.to_string(), + }; + out.push_str(&rewritten); + out.push('\n'); + } + std::fs::write(path, out).expect("write the regenerated corpus"); + println!("regenerated {path}"); +} diff --git a/platform/script/tests/query_api_surface.rs b/platform/script/tests/query_api_surface.rs new file mode 100644 index 0000000000..f34172df77 --- /dev/null +++ b/platform/script/tests/query_api_surface.rs @@ -0,0 +1,65 @@ +//! The stability promise for the query API's public surface — ADR 0001 §4, +//! App Flow #9. +//! +//! `makepad-script` is at 1.0.0 with no CI on this fork and no crates.io +//! release, so "semver" is a discipline this repository keeps by hand. Two +//! mechanisms, and nothing that needs a CI runner: +//! +//! * **The compiler is the gate.** The signatures below are bound to consts. +//! Removing, renaming or reshaping either function — moving it out of +//! `query`, changing a parameter or a return type — stops this file +//! compiling, so the break cannot land silently. +//! * **The checked-in surface file is the review artifact.** It must keep +//! describing the API, so an intentional change updates the file and the +//! pull request names the diff. Together with the fixture corpus +//! (`tests/query_corpus.txt`), that is the equivalent of a semver check for +//! a crate nobody publishes. +//! +//! Within 1.x the rule is additive only: new functions may appear. Changing +//! the order `object_keys` returns is breaking too — the corpus pins it. + +use makepad_script::query; +use makepad_script::*; + +// The promised surface, spelled so the compiler checks it. +const OBJECT_KEYS: fn(&ScriptHeap, ScriptObject) -> Vec = query::object_keys; +const MODULE_OBJECT: fn(&ScriptHeap, &str) -> Option = query::module_object; + +/// The same surface as the checked-in text a reviewer reads. +const SURFACE: &str = "makepad_script::query::object_keys(heap: &ScriptHeap, object: ScriptObject) -> Vec\nmakepad_script::query::module_object(heap: &ScriptHeap, name: &str) -> Option"; + +fn test_vm() -> ScriptVm<'static> { + let host = Box::leak(Box::new(ScriptVmHost::new((), ()))); + ScriptVm { + host, + bx: Box::new(ScriptVmBase::new()), + } +} + +#[test] +fn the_promised_surface_is_reachable_exactly_as_spelled() { + // The runtime half of the gate: the consts compile (else this file does + // not) and the two functions answer through them. + let vm = test_vm(); + assert!(MODULE_OBJECT(&vm.bx.heap, "mod.std").is_some()); + assert!(MODULE_OBJECT(&vm.bx.heap, "nope").is_none()); + let std = MODULE_OBJECT(&vm.bx.heap, "mod.std").unwrap(); + assert!(OBJECT_KEYS(&vm.bx.heap, std).contains(&"print".to_string())); +} + +#[test] +fn the_checked_in_surface_file_describes_the_api() { + let path = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/query_api_surface.txt"); + let checked_in = std::fs::read_to_string(path).expect("query_api_surface.txt is checked in beside this test"); + let promised: Vec<&str> = checked_in + .lines() + .map(|line| line.trim()) + .filter(|line| !line.is_empty() && !line.starts_with('#')) + .collect(); + assert_eq!( + promised.join("\n"), + SURFACE, + "the checked-in surface file must describe the API: update it, and name \ + the change in the pull request, whenever the surface above changes" + ); +} diff --git a/platform/script/tests/query_api_surface.txt b/platform/script/tests/query_api_surface.txt new file mode 100644 index 0000000000..90f6227838 --- /dev/null +++ b/platform/script/tests/query_api_surface.txt @@ -0,0 +1,20 @@ +# The VM query API's promised public surface — ADR 0001 §4, App Flow #9. +# +# Within 1.x this surface is additive only: new functions may appear; removing +# or reinterpreting either function, or changing the order `object_keys` +# returns, is 2.0 and is called out in the fork's release notes. +# +# The fork has no CI and makepad-script is not published to crates.io, so this +# promise is kept by hand and gated by tests/query_api_surface.rs, which runs +# with `cargo test -p makepad-script`: +# +# * it fails to COMPILE if either function is removed, renamed or reshaped +# (the signatures below are bound to consts in the test), and +# * it fails the TEST if this file stops describing the API, so a breaking +# change cannot land without the diff being named in the pull request. +# +# The fixture corpus (tests/query_corpus.txt) is the other half of the promise: +# it records the objects each function answers about, so a change to a module's +# members is a visible diff too. +makepad_script::query::object_keys(heap: &ScriptHeap, object: ScriptObject) -> Vec +makepad_script::query::module_object(heap: &ScriptHeap, name: &str) -> Option diff --git a/platform/script/tests/query_corpus.txt b/platform/script/tests/query_corpus.txt new file mode 100644 index 0000000000..f6f0ae6ee7 --- /dev/null +++ b/platform/script/tests/query_corpus.txt @@ -0,0 +1,43 @@ +# The VM query API fixture corpus — ADR 0001 §4, App Flow #9. +# +# One entry per line: +# The name is what `module_object` answers to; the keys are what `object_keys` +# must answer for it, sorted, each name once; the total is the object's full +# entry count, named or not. A module this VM does not install is drift, and so +# is a key set that moved. +# +# The total matters because a name is not always recoverable: a key registered +# in Rust with `id!` and never spelled by any source is an id the VM cannot +# turn back into a string — the same gap its own "Did you mean" lists have — +# and `object_keys` does not invent one. Recording the total keeps such an +# entry visible: a key the corpus cannot name is still a diff. +# +# The runner names the crate whose test checks the entry, because the modules a +# real isolate installs span three crates and no single `cargo test` builds +# them all: +# +# script — installed by makepad-script itself (a bare `ScriptVmBase`). +# Checked by `cargo test -p makepad-script` +# (platform/script/tests/query_api_corpus.rs). +# widgets — installed by a Splash isolate's widget-facing setup (the widget +# registry, the host and storage modules, the sys module, and the +# Cx-free runtime's net). Checked by +# `cargo test -p makepad-widgets` +# (widgets/tests/vm_query_corpus.rs), which reads this same file. +# +# The corpus is the review artifact for the query API's stability promise: the +# fork has no CI and makepad-script is not on crates.io, so a change to either +# function — or to a module's members — shows up here as a diff a person names +# in the pull request. Regenerate with QUERY_CORPUS_WRITE=1, then read the diff. +mod.gc script dump_tag,run,run_status,set_static 4 +mod.math script E,GOLDEN,LN10,LN2,LOG10E,LOG2E,PI,SQRT1_2,TAU,TORAD,abs,acos,acosh,asfloat,asin,asinh,asint,asuint,atan,atan2,atanh,ceil,clamp,cos,cosh,cross,dFdx,dFdy,degrees,discard,distance,dot,exp,exp2,floor,fma,fract,instance_index,inverse,inverseSqrt,length,lerp,log,log2,max,min,mix,modf,normalize,pow,radians,round,sign,sin,sinh,smoothstep,sqrt,step,tan,tanh,trunc,unpack2f16,unpack4u8 63 +mod.pod script array,atomic_u32,bool,f16,f16x2,f16x4,f32,float,i16x2,i32,int,mat2x2f,mat2x3f,mat2x4f,mat3x2f,mat3x3f,mat3x4f,mat4x2f,mat4x3f,mat4x4f,pod_atomic_i32,snorm16x2,snorm8x4,struct,u16x2,u32,uint,unorm16x2,unorm8x4,vec2,vec2b,vec2f,vec2h,vec2i,vec2u,vec3,vec3b,vec3f,vec3h,vec3i,vec3u,vec4,vec4b,vec4f,vec4h,vec4i,vec4u,void 48 +mod.shader script depth_clip,fragment_output,instance,test_compile_draw,test_compile_draw_const_table,test_compile_draw_contains,test_compile_draw_errors,test_compile_draw_rust_contains,test_compile_draw_source,texture_1d,texture_1d_array,texture_2d,texture_2d_array,texture_3d,texture_3d_array,texture_cube,texture_cube_array,texture_depth,texture_depth_array,texture_video,uniform,uniform_buffer,varying,vertex_buffer,vertex_position 25 +mod.std script Range,assert,log,print,println,regex,set_type_default 7 +mod.agent widgets notify 1 +mod.fs widgets append,exists,list,mkdir,read,read_bytes,remove,write 8 +mod.host widgets capabilities,has,request 3 +mod.net widgets HttpEvents,HttpMethod,HttpRequest,HttpServerEvents,HttpServerHeaders,HttpServerOptions,HttpServerResponse,SocketStreamOptions,WebSocketEvents,http_request,http_server,socket_stream,web_socket 13 +mod.prelude.widgets widgets AdaptiveView,AdaptiveViewBase,AiChatSlot,AiChatSlotBase,Align,AllCharacters,AnimatedImageGif,AnimatedImageGifBase,Animator,AnimatorGroup,AnimatorState,AppIcon,AppleGlassRoundedView,AqiContour,AqiContourBase,AreaChart,AreaChartBase,Ascii,AutoCapitalize,AutoCorrect,AutoFlow,BarChart,BarChartBase,BareStep,Base,BelowInput,Bezier,BounceLoop,Button,ButtonBase,ButtonFlat,ButtonFlatIcon,ButtonFlatter,ButtonFlatterIcon,ButtonGradientX,ButtonGradientXIcon,ButtonGradientY,ButtonGradientYIcon,ButtonIcon,CachedRoundedView,CachedScrollX,CachedScrollXY,CachedScrollY,CachedView,CachedWidget,CalloutTooltip,CalloutTooltipInner,CameraPreview,CameraPreviewBase,CamoTrackRow,CandlestickChart,CandlestickChartBase,CellAlign,CellPlacement,Center,ChartView,ChartViewBase,CheckBox,CheckBoxBase,CheckBoxCustom,CheckBoxFlat,Circle,CircleView,Clip,ComboBox,ComboBoxBase,ComboBoxFlat,Constant,Continue,DataGrid,DataGridBase,Decimal,Default,DesktopButton,DesktopButtonBase,DesktopButtonType,Disabled,Distribute,Dock,DockBase,DockFlat,DockSplitter,DockTab,DockTabs,Done,Down,DragAxis,DrawComboItemBgBase,DrawComboItemTextBase,DrawDropDown2ArrowBase,DrawDropDown2ItemBgBase,DrawDropDown2ItemTextBase,DrawDropDown2LabelBase,DrawFlowBlock,DrawLabelTextBase,DrawMouseCursor,DrawRoundCorner,DrawScrollShadow,DropDown,DropDown2,DropDown2Base,DropDown2Flat,DropDownBase,DropDownFlat,DropDownGradientX,DropDownGradientY,DropShadow,DropSlider,DropSliderBase,E,Ease,Ellipse,Ellipsis,Email,EmailAddress,EmergencyCall,Emphasized,Enabled,EventOrder,ExpDecay,ExpandablePanel,ExpandablePanelBase,Expr,FabColorPick,FabColorPickBase,FabColorWheel,FabColorWheelBase,FabHeaderLabel,FabLabel,FabLabelDim,FabLabelSmall,FabPaletteStrip,FabPaletteStripBase,FabPropRow,FabSearch,FabSection,FabValueInput,FabValueInputBase,FileTree,FileTreeBase,FileTreeNode,FileTreeNodeBase,Fill,Filler,Filter,Fit,FitBound,Fixed,FlatList,FlatListBase,Flow,FlowBlockType,FoldButton,FoldButtonBase,FoldHeader,FoldHeaderBase,FontFamily,FontMember,Forward,FromA,FromB,FullStreetAddress,GOLDEN,GaussGradientRoundedView,GaussRoundedView,GaussRoundedViewBase,GaussShadow,GestureView,GestureViewBase,GitStatusDotKind,GlassPanel,Go,Google,Gradient,GradientXView,GradientYView,Grid,GridBase,Group,H1,H1italic,H2,H2italic,H3,H3italic,H4,H4italic,HCenter,HexagonView,Horizontal,HostedView,HostedViewBase,Hr,Html,HtmlBase,HtmlLink,HtmlLinkBase,Icon,IconBase,IconGradientX,IconGradientY,IconRotated,IconSet,Image,ImageBase,ImageBlend,ImageBlendBase,ImageFit,InBack,InBounce,InCirc,InCubic,InElastic,InExp,InOutBack,InOutBounce,InOutCirc,InOutCubic,InOutElastic,InOutExp,InOutQuad,InOutQuart,InOutQuint,InOutSine,InQuad,InQuart,InQuint,InSine,IndicatorPlot,IndicatorPlotBase,InputMode,Inset,InsetPanelView,Join,KeyboardView,KeyboardViewBase,KitBottomNavigation,KitButton,KitFormField,KitSelectionControl,KitTabBar,LN10,LN2,LOG10E,LOG2E,Label,LabelBase,LabelGradientX,LabelGradientY,Labelbold,Layout,Left,LegendPosition,Line,LineChart,LineChartBase,LineStyle,Linear,LinkLabel,LinkLabelBase,LinkLabelIcon,LoadingSpinner,Loop,Markdown,MarkdownBase,MarkdownLink,MarkdownLinkBase,MarkerStyle,Math,MathView,MathViewBase,MenuBar,MenuBarBase,MenuItem,Metrics,Modal,ModalBase,MoonPhase,MouseCursor,NavControl,NavControlBase,NewPassword,Next,None,Numeric,OhlcChart,OhlcChartBase,OnSelected,OneTimeCode,OutBack,OutBounce,OutCirc,OutCubic,OutElastic,OutExp,OutQuad,OutQuart,OutQuint,OutSine,Overlay,P,PI,PageFlip,PageFlipBase,Pal,PanelView,Password,Path,Pbold,Pbolditalic,PerfGraph,PerfGraphBase,Pitalic,Play,PlotView,PlotViewBase,Polygon,Polyline,PopupMenu,PopupMenuBase,PopupMenuFlat,PopupMenuGradientX,PopupMenuGradientY,PopupMenuItem,PopupMenuItemBase,PopupMenuItemGradientX,PopupMenuItemGradientY,PopupMenuPosition,PopupNotification,PopupNotificationBase,PortalList,PortalListBase,Pow,Previous,RadGradient,RadioButton,RadioButtonBase,RadioButtonFlat,RadioButtonFlatter,RadioButtonTab,RadioButtonTabFlat,Range,Rect,RectShadowView,RectView,Rel,ReorderList,ReorderListBase,RepeatMode,ReturnKeyType,Reverse,ReverseLoop,Right,Root,RootBase,Rotary,RotaryFlat,RotaryGradientY,Rotate,RoundedAllView,RoundedShadowView,RoundedView,RoundedXView,RoundedYView,Route,RowAlign,RubberView,RubberViewBase,SQRT1_2,Scale,ScaleType,ScatterChart,ScatterChartBase,ScreenCap,ScreenCapBase,ScrollBar,ScrollBarBase,ScrollBarTabs,ScrollBars,ScrollBarsBase,ScrollBarsTabs,ScrollXView,ScrollXYView,ScrollYView,Sdf2d,Search,Send,Sentences,SheetView,SheetViewBase,Size,SkewX,SkewY,Slide,SlideBody,SlideChapter,SlidePanel,SlidePanelBase,SlideSide,Slider,SliderBase,SliderFlat,SliderGradientX,SliderGradientY,SliderMinimal,SliderMinimalFlat,SliderRound,SliderRoundFlat,SliderRoundGradientX,SliderRoundGradientY,SlidesView,SlidesViewBase,Snap,SolidView,Sparkline,SparklineBase,Splash,SplashBase,Splitter,SplitterAlign,SplitterAxis,SplitterBase,StackNavigation,StackNavigationBase,StackNavigationView,StackNavigationViewBase,StackViewHeader,StepStyle,StockPlot,StockPlotBase,Stop,SunArc,Svg,SvgBase,TAU,TORAD,Tab,TabBar,TabBarBase,TabBarFlat,TabBarGradientX,TabBarGradientY,TabBase,TabCloseButton,TabCloseButtonBase,TabFlat,TabGradientX,TabGradientY,TaskplanProjectCard,Tel,TelephoneNumber,TempBar,Text,TextBody,TextBox,TextCaption,TextFlow,TextFlowBase,TextFlowLink,TextFlowLinkBase,TextHero,TextInput,TextInputBase,TextInputContentType,TextInputFlat,TextInputGradientX,TextInputGradientY,TextOverflow,TextRow,TextStat,TextStyle,TextTitle,TextValue,Tip,TipBase,TipLayer,TipLayerBase,Toggle,ToggleFlat,Tooltip,TooltipBase,Top,TopLeft,Track,TrackLen,Translate,TrendChart,TrendChartBase,TurtleStep,TweakMaterialSwatch,TweakMaterialSwatchBase,Tweaker,TweakerBase,Tween,Url,Username,VCenter,ValueInput,ValueInputBase,Vector,VectorBase,Vertical,Video,VideoBase,VideoDataSource,View,ViewBase,VoiceWave,Vr,Walk,WeatherIcon,WebCard,WebCardBase,WebReader,WebReaderBase,Weighted,Window,WindowBase,WindowMenu,WindowMenuBase,Words,Yahoo,abs,acos,acosh,array,asfloat,asin,asinh,asint,assert,asuint,atan,atan2,atanh,atomic_u32,binary_resource,bool,ceil,clamp,cos,cosh,crate_resource,cross,dFdx,dFdy,degrees,depth_clip,discard,distance,dot,draw,exp,exp2,f16,f16x2,f16x4,f32,file_resource,float,floor,fma,fract,fragment_output,glass,http_resource,i16x2,i32,instance,instance_index,int,inverse,inverseSqrt,length,lerp,load_all_resources,local_time,log,log2,mat2x2f,mat2x3f,mat2x4f,mat3x2f,mat3x3f,mat3x4f,mat4x2f,mat4x3f,mat4x4f,max,min,mix,modf,normalize,pod_atomic_i32,pow,print,println,promise,radians,random,random_seed,random_u32,regex,round,set_type_default,sign,sin,sinh,smoothstep,snap,snorm16x2,snorm8x4,sqrt,start_interval,start_timeout,startup,step,stop_timer,struct,tan,tanh,task,test_compile_draw,test_compile_draw_const_table,test_compile_draw_contains,test_compile_draw_errors,test_compile_draw_rust_contains,test_compile_draw_source,texture_1d,texture_1d_array,texture_2d,texture_2d_array,texture_3d,texture_3d_array,texture_cube,texture_cube_array,texture_depth,texture_depth_array,texture_video,theme,time_now,trunc,u16x2,u32,uint,uniform,uniform_buffer,unorm16x2,unorm8x4,unpack2f16,unpack4u8,varying,vec2,vec2b,vec2f,vec2h,vec2i,vec2u,vec3,vec3b,vec3f,vec3h,vec3i,vec3u,vec4,vec4b,vec4f,vec4h,vec4i,vec4u,vertex_buffer,vertex_position,void 678 +mod.sys widgets airmap,airquality,aqigrid,aqinum,basemap,cities,citiesnum,citytime,citytimenum,convert,coord,dataset,daylight,dayname,fetch,fmtdur,fmtnum,fx,fxnum,geocode,geocodenum,gps,indicator,json_string,l0_math,l0_ratio,link,locale,mappin,maptile,moonnum,moonphase,movers,navroute,navroutenum,navsecs,navstep,navstepnum,news,news_digest,newsitem,num,photo,places,placesnum,poi,prefs,quakes,quakesnum,reading,route,satellite,satellite_ir,search,searchnum,simsecs,stock,stockbar,stockrange,symbol_search,symbol_searchnum,topics,video,videonum,watchlist,watchlist_has,watchlistnum,weather,weathercond,weathernum,weatherword,weekmax,weekmin 73 +mod.widgets widgets AdaptiveView,AdaptiveViewBase,AiChatSlot,AiChatSlotBase,AnimatedImageGif,AnimatedImageGifBase,AppIcon,AppleGlassRoundedView,AqiContour,AqiContourBase,AreaChart,AreaChartBase,AutoFlow,BarChart,BarChartBase,BareStep,BelowInput,Button,ButtonBase,ButtonFlat,ButtonFlatIcon,ButtonFlatter,ButtonFlatterIcon,ButtonGradientX,ButtonGradientXIcon,ButtonGradientY,ButtonGradientYIcon,ButtonIcon,CachedRoundedView,CachedScrollX,CachedScrollXY,CachedScrollY,CachedView,CachedWidget,CalloutTooltip,CalloutTooltipInner,CameraPreview,CameraPreviewBase,CamoTrackRow,CandlestickChart,CandlestickChartBase,ChartView,ChartViewBase,CheckBox,CheckBoxBase,CheckBoxCustom,CheckBoxFlat,Circle,CircleView,ComboBox,ComboBoxBase,ComboBoxFlat,DataGrid,DataGridBase,DesktopButton,DesktopButtonBase,DesktopButtonType,Dock,DockBase,DockFlat,DockSplitter,DockTab,DockTabs,DragAxis,DrawComboItemBgBase,DrawComboItemTextBase,DrawDropDown2ArrowBase,DrawDropDown2ItemBgBase,DrawDropDown2ItemTextBase,DrawDropDown2LabelBase,DrawFlowBlock,DrawLabelTextBase,DrawMouseCursor,DrawRoundCorner,DrawScrollShadow,DropDown,DropDown2,DropDown2Base,DropDown2Flat,DropDownBase,DropDownFlat,DropDownGradientX,DropDownGradientY,DropShadow,DropSlider,DropSliderBase,Ellipse,EventOrder,ExpandablePanel,ExpandablePanelBase,FabColorPick,FabColorPickBase,FabColorWheel,FabColorWheelBase,FabHeaderLabel,FabLabel,FabLabelDim,FabLabelSmall,FabPaletteStrip,FabPaletteStripBase,FabPropRow,FabSearch,FabSection,FabValueInput,FabValueInputBase,FileTree,FileTreeBase,FileTreeNode,FileTreeNodeBase,Filler,Filter,FlatList,FlatListBase,FlowBlockType,FoldButton,FoldButtonBase,FoldHeader,FoldHeaderBase,FromA,FromB,GaussGradientRoundedView,GaussRoundedView,GaussRoundedViewBase,GestureView,GestureViewBase,GitStatusDotKind,GlassPanel,Gradient,GradientXView,GradientYView,Grid,GridBase,Group,H1,H1italic,H2,H2italic,H3,H3italic,H4,H4italic,HexagonView,Horizontal,HostedView,HostedViewBase,Hr,Html,HtmlBase,HtmlLink,HtmlLinkBase,Icon,IconBase,IconGradientX,IconGradientY,IconRotated,IconSet,Image,ImageBase,ImageBlend,ImageBlendBase,ImageFit,IndicatorPlot,IndicatorPlotBase,InsetPanelView,KeyboardView,KeyboardViewBase,KitBottomNavigation,KitButton,KitFormField,KitSelectionControl,KitTabBar,Label,LabelBase,LabelGradientX,LabelGradientY,Labelbold,Left,LegendPosition,Line,LineChart,LineChartBase,LineStyle,LinkLabel,LinkLabelBase,LinkLabelIcon,LoadingSpinner,Markdown,MarkdownBase,MarkdownLink,MarkdownLinkBase,MarkerStyle,MathView,MathViewBase,MenuBar,MenuBarBase,MenuItem,Modal,ModalBase,MoonPhase,NavControl,NavControlBase,OhlcChart,OhlcChartBase,OnSelected,P,PageFlip,PageFlipBase,PanelView,Path,Pbold,Pbolditalic,PerfGraph,PerfGraphBase,Pitalic,PlotView,PlotViewBase,Polygon,Polyline,PopupMenu,PopupMenuBase,PopupMenuFlat,PopupMenuGradientX,PopupMenuGradientY,PopupMenuItem,PopupMenuItemBase,PopupMenuItemGradientX,PopupMenuItemGradientY,PopupMenuPosition,PopupNotification,PopupNotificationBase,PortalList,PortalListBase,RadGradient,RadioButton,RadioButtonBase,RadioButtonFlat,RadioButtonFlatter,RadioButtonTab,RadioButtonTabFlat,Rect,RectShadowView,RectView,ReorderList,ReorderListBase,RepeatMode,Right,Root,RootBase,Rotary,RotaryFlat,RotaryGradientY,Rotate,RoundedAllView,RoundedShadowView,RoundedView,RoundedXView,RoundedYView,RubberView,RubberViewBase,Scale,ScaleType,ScatterChart,ScatterChartBase,ScreenCap,ScreenCapBase,ScrollBar,ScrollBarBase,ScrollBarTabs,ScrollBars,ScrollBarsBase,ScrollBarsTabs,ScrollXView,ScrollXYView,ScrollYView,SheetView,SheetViewBase,SkewX,SkewY,Slide,SlideBody,SlideChapter,SlidePanel,SlidePanelBase,SlideSide,Slider,SliderBase,SliderFlat,SliderGradientX,SliderGradientY,SliderMinimal,SliderMinimalFlat,SliderRound,SliderRoundFlat,SliderRoundGradientX,SliderRoundGradientY,SlidesView,SlidesViewBase,SolidView,Sparkline,SparklineBase,Splash,SplashBase,Splitter,SplitterAlign,SplitterAxis,SplitterBase,StackNavigation,StackNavigationBase,StackNavigationView,StackNavigationViewBase,StackViewHeader,StepStyle,StockPlot,StockPlotBase,Stop,SunArc,Svg,SvgBase,Tab,TabBar,TabBarBase,TabBarFlat,TabBarGradientX,TabBarGradientY,TabBase,TabCloseButton,TabCloseButtonBase,TabFlat,TabGradientX,TabGradientY,TaskplanProjectCard,TempBar,TextBody,TextBox,TextCaption,TextFlow,TextFlowBase,TextFlowLink,TextFlowLinkBase,TextHero,TextInput,TextInputBase,TextInputFlat,TextInputGradientX,TextInputGradientY,TextRow,TextStat,TextTitle,TextValue,Tip,TipBase,TipLayer,TipLayerBase,Toggle,ToggleFlat,Tooltip,TooltipBase,Top,Track,TrackLen,Translate,TrendChart,TrendChartBase,TurtleStep,TweakMaterialSwatch,TweakMaterialSwatchBase,Tweaker,TweakerBase,Tween,ValueInput,ValueInputBase,Vector,VectorBase,Vertical,Video,VideoBase,VideoDataSource,View,ViewBase,VoiceWave,Vr,WeatherIcon,WebCard,WebCardBase,WebReader,WebReaderBase,Weighted,Window,WindowBase,WindowMenu,WindowMenuBase,glass 396 diff --git a/platform/src/cx_api.rs b/platform/src/cx_api.rs index 363dc7eeb3..82f6e613a1 100644 --- a/platform/src/cx_api.rs +++ b/platform/src/cx_api.rs @@ -1219,6 +1219,18 @@ impl Cx { if let crate::script::res::CxScriptResourceData::Loaded(data) = &res.data { return Some(SharedBytes::from_owned(data.clone())); } + // `path` is where the resource lived when it was registered: the build + // machine's directory. On any other machine the font is only in the + // package's resource tree, under the name `dependency_path` gives it + // inside its crate -- the same place `load_packaged_resource` searches + // for an unpackaged build's other assets. + if let Some(dep_path) = res.dependency_path.as_deref() { + if let Some(bytes) = crate::os::cx_native::read_file_cwd_or_exe_relative(format!( + "resources/{dep_path}" + )) { + return Some(SharedBytes::from_vec(bytes)); + } + } res.dependency_path .as_deref() .and_then(|path| self.get_dependency(path).ok()) diff --git a/platform/src/os/linux/open_harmony/mod.rs b/platform/src/os/linux/open_harmony/mod.rs index a8917d9ae7..d40aad441f 100644 --- a/platform/src/os/linux/open_harmony/mod.rs +++ b/platform/src/os/linux/open_harmony/mod.rs @@ -1,5 +1,8 @@ pub mod arkts_obj_ref; pub mod oh_callbacks; +pub mod oh_env; +pub mod oh_ime; +pub mod oh_web; pub mod oh_camera; pub mod oh_media; pub mod oh_sys; diff --git a/platform/src/os/linux/open_harmony/node_shim.cpp b/platform/src/os/linux/open_harmony/node_shim.cpp new file mode 100644 index 0000000000..d675ffeb5a --- /dev/null +++ b/platform/src/os/linux/open_harmony/node_shim.cpp @@ -0,0 +1,78 @@ +// makepad's one ArkUI node, created natively (NDK) instead of by ArkTS. +// +// ArkTS hands over a NodeContent slot (a ContentSlot on the page); this file +// puts makepad's XComponent into it and listens to it natively: axis events +// (mouse wheel, two-finger touchpad), and the node's UI context, which the +// native input method needs to attach. Everything else about the XComponent +// (surface, touch, mouse, keys) is registered from Rust on the +// OH_NativeXComponent this returns. +// +// A shim rather than Rust bindings because ArkUI_NativeNodeAPI_1 is a table of +// function pointers whose order is the ABI: the SDK header checks it here. +// C++ because the SDK's ArkUI headers need a C++ translation unit. +#include + +#include +#include +#include +#include +#include +#include +#include + +extern "C" void makepad_ohos_on_axis(double dx, double dy, float x, float y, int32_t tool); + +static ArkUI_NativeNodeAPI_1 *g_api = nullptr; + +static void on_node_event(ArkUI_NodeEvent *event) { + if (OH_ArkUI_NodeEvent_GetEventType(event) != NODE_ON_AXIS) { + return; + } + ArkUI_UIInputEvent *input = OH_ArkUI_NodeEvent_GetInputEvent(event); + if (input == nullptr) { + return; + } + makepad_ohos_on_axis(OH_ArkUI_AxisEvent_GetHorizontalAxisValue(input), + OH_ArkUI_AxisEvent_GetVerticalAxisValue(input), + OH_ArkUI_PointerEvent_GetX(input), OH_ArkUI_PointerEvent_GetY(input), + OH_ArkUI_UIInputEvent_GetToolType(input)); +} + +// 0 on success; the XComponent and the UI context come back through the out +// pointers. Must run on the ArkTS main thread (it is called from napi). +extern "C" int makepad_ohos_mount(napi_env env, napi_value content_value, OH_NativeXComponent **out_xcomponent, + ArkUI_ContextHandle *out_context) { + OH_ArkUI_GetModuleInterface(ARKUI_NATIVE_NODE, ArkUI_NativeNodeAPI_1, g_api); + if (g_api == nullptr) { + return -1; + } + ArkUI_NodeContentHandle content = nullptr; + if (OH_ArkUI_GetNodeContentFromNapiValue(env, content_value, &content) != 0 || content == nullptr) { + return -2; + } + ArkUI_NodeHandle node = g_api->createNode(ARKUI_NODE_XCOMPONENT); + if (node == nullptr) { + return -3; + } + ArkUI_AttributeItem id = {nullptr, 0, "makepad", nullptr}; + g_api->setAttribute(node, NODE_XCOMPONENT_ID, &id); + ArkUI_NumberValue type[] = {{.i32 = ARKUI_XCOMPONENT_TYPE_SURFACE}}; + ArkUI_AttributeItem type_item = {type, 1, nullptr, nullptr}; + g_api->setAttribute(node, NODE_XCOMPONENT_TYPE, &type_item); + ArkUI_NumberValue full[] = {{.f32 = 1.0f}}; + ArkUI_AttributeItem full_item = {full, 1, nullptr, nullptr}; + g_api->setAttribute(node, NODE_WIDTH_PERCENT, &full_item); + g_api->setAttribute(node, NODE_HEIGHT_PERCENT, &full_item); + ArkUI_NumberValue yes[] = {{.i32 = 1}}; + ArkUI_AttributeItem yes_item = {yes, 1, nullptr, nullptr}; + g_api->setAttribute(node, NODE_FOCUSABLE, &yes_item); + g_api->setAttribute(node, NODE_DEFAULT_FOCUS, &yes_item); + g_api->registerNodeEventReceiver(on_node_event); + g_api->registerNodeEvent(node, NODE_ON_AXIS, 0, nullptr); + if (OH_ArkUI_NodeContent_AddNode(content, node) != 0) { + return -4; + } + *out_xcomponent = OH_NativeXComponent_GetNativeXComponent(node); + *out_context = OH_ArkUI_GetContextByNode(node); + return *out_xcomponent == nullptr ? -5 : 0; +} diff --git a/platform/src/os/linux/open_harmony/oh_callbacks.rs b/platform/src/os/linux/open_harmony/oh_callbacks.rs index e59556bb81..eb46f440a7 100644 --- a/platform/src/os/linux/open_harmony/oh_callbacks.rs +++ b/platform/src/os/linux/open_harmony/oh_callbacks.rs @@ -5,7 +5,14 @@ use crate::makepad_math::*; use napi_derive_ohos::napi; use napi_ohos::{Env, JsObject, JsString, NapiRaw}; use ohos_sys::xcomponent::{ - OH_NativeXComponent, OH_NativeXComponent_Callback, OH_NativeXComponent_GetTouchEvent, + OH_NativeXComponent, OH_NativeXComponent_Callback, OH_NativeXComponent_GetMouseEvent, + OH_NativeXComponent_GetTouchEvent, OH_NativeXComponent_GetTouchPointToolType, + OH_NativeXComponent_MouseEvent, OH_NativeXComponent_MouseEventAction, + OH_NativeXComponent_MouseEvent_Callback, OH_NativeXComponent_RegisterMouseEventCallback, + OH_NativeXComponent_TouchPointToolType, OH_NativeXComponent_GetKeyEvent, + OH_NativeXComponent_GetKeyEventAction, OH_NativeXComponent_GetKeyEventCode, + OH_NativeXComponent_KeyAction, OH_NativeXComponent_KeyCode, OH_NativeXComponent_KeyEvent, + OH_NativeXComponent_RegisterKeyEventCallback, OH_NativeXComponent_GetXComponentSize, OH_NativeXComponent_RegisterCallback, OH_NativeXComponent_TouchEvent, OH_NativeXComponent_TouchEventType, }; @@ -65,22 +72,44 @@ pub fn send_from_ohos_message(message: FromOhosMessage) { } } +/// ArkTS's whole part in makepad's UI: it hands over one `NodeContent` slot +/// (a `ContentSlot` on the page) and native code puts the XComponent in it +/// (`node_shim.cpp`), registers its callbacks and takes its UI context for +/// the native input method. #[napi] -pub fn handle_insert_text_event(text: String) -> napi_ohos::Result<()> { - let e = TextInputEvent { - input: text, - replace_last: false, - was_paste: false, - ..Default::default() - }; - send_from_ohos_message(FromOhosMessage::TextInput(e)); +pub fn mount_content(env: Env, content: JsObject) -> napi_ohos::Result<()> { + extern "C" { + fn makepad_ohos_mount( + env: napi_ohos::sys::napi_env, + content: napi_ohos::sys::napi_value, + out_xcomponent: *mut *mut OH_NativeXComponent, + out_context: *mut *mut c_void, + ) -> i32; + } + let mut xcomponent: *mut OH_NativeXComponent = core::ptr::null_mut(); + let mut context: *mut c_void = core::ptr::null_mut(); + let res = unsafe { makepad_ohos_mount(env.raw(), content.raw(), &mut xcomponent, &mut context) }; + if res != 0 { + crate::error!("mount_content: makepad_ohos_mount failed: {res}"); + return Ok(()); + } + register_native_xcomponent_callbacks(xcomponent); + super::oh_ime::set_context(context); Ok(()) } -#[napi] -pub fn handle_delete_left_event(length: i32) -> napi_ohos::Result<()> { - send_from_ohos_message(FromOhosMessage::DeleteLeft(length)); - Ok(()) +/// A scroll from the XComponent node's NODE_ON_AXIS (mouse wheel, two-finger +/// touchpad), from `node_shim.cpp`; positive scrolls down/right. +#[no_mangle] +extern "C" fn makepad_ohos_on_axis(dx: f64, dy: f64, x: f32, y: f32, tool: i32) { + static LOGGED: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); + if LOGGED.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 6 { + crate::log!("ohos axis: dx={dx} dy={dy} at {x},{y} tool={tool}"); + } + if dx != 0.0 || dy != 0.0 { + // UI_INPUT_EVENT_TOOL_TYPE_MOUSE is 3 (a wheel); 4 is a touchpad. + send_from_ohos_message(FromOhosMessage::Scroll { scroll: dvec2(dx, dy), is_mouse: tool == 3 }); + } } #[napi] @@ -200,6 +229,14 @@ extern "C" fn on_dispatch_touch_event_cb(component: *mut OH_NativeXComponent, wi let mut touches = Vec::with_capacity(touch_event.numPoints as usize); for idx in 0..touch_event.numPoints { + // A mouse or touchpad arrives through `on_dispatch_mouse_event_cb` + // as mouse events; its synthesized touch would click twice. + let mut tool = OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_UNKNOWN; + if unsafe { OH_NativeXComponent_GetTouchPointToolType(component, idx, &mut tool) } == 0 + && tool == OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_MOUSE + { + continue; + } let point = &(touch_event.touchPoints[idx as usize]); let touch_state = match point.type_ { OH_NativeXComponent_TouchEventType::OH_NATIVEXCOMPONENT_DOWN => TouchState::Start, @@ -226,10 +263,75 @@ extern "C" fn on_dispatch_touch_event_cb(component: *mut OH_NativeXComponent, wi sweep_lock: Cell::new(Area::Empty), }) } - send_from_ohos_message(FromOhosMessage::Touch(touches)); + if !touches.is_empty() { + send_from_ohos_message(FromOhosMessage::Touch(touches)); + } //crate::log!("OnDispatchTouchEventCallBack"); } +/// A mouse or touchpad on the XComponent (a 2-in-1 or a tablet with a +/// pointer): press, release and motion, in the same pixels as touches. +#[no_mangle] +extern "C" fn on_dispatch_mouse_event_cb(component: *mut OH_NativeXComponent, window: *mut c_void) { + let mut mouse_event: MaybeUninit = MaybeUninit::uninit(); + let res = unsafe { OH_NativeXComponent_GetMouseEvent(component, window, mouse_event.as_mut_ptr()) }; + if res != 0 { + crate::error!("OH_NativeXComponent_GetMouseEvent failed with {res}"); + return; + } + let e = unsafe { mouse_event.assume_init() }; + let action = match e.action { + OH_NativeXComponent_MouseEventAction::OH_NATIVEXCOMPONENT_MOUSE_PRESS => MouseAction::Down, + OH_NativeXComponent_MouseEventAction::OH_NATIVEXCOMPONENT_MOUSE_RELEASE => MouseAction::Up, + OH_NativeXComponent_MouseEventAction::OH_NATIVEXCOMPONENT_MOUSE_MOVE => MouseAction::Move, + _ => return, + }; + send_from_ohos_message(FromOhosMessage::Mouse { + action, + // OpenHarmony's button values are makepad's MouseButton bits. + button: e.button.0, + abs: dvec2(e.x as f64, e.y as f64), + time: e.timestamp as f64 / 1000000000.0, + }); +} + +#[link(name = "ace_ndk.z")] +extern "C" { + /// API 20: Ctrl/Shift/Alt/Fn held with this key (ARKUI_MODIFIER_KEY_*). + fn OH_NativeXComponent_GetKeyEventModifierKeyStates( + key_event: *mut OH_NativeXComponent_KeyEvent, + keys: *mut u64, + ) -> i32; +} + +/// A hardware key on the focused XComponent. Text still arrives through the +/// input method (`insertText`); this carries what a text path cannot: +/// Enter, Tab, Escape, arrows, paging, function keys and modifier chords. +#[no_mangle] +extern "C" fn on_dispatch_key_event_cb(component: *mut OH_NativeXComponent, _window: *mut c_void) { + let mut event: *mut OH_NativeXComponent_KeyEvent = core::ptr::null_mut(); + if unsafe { OH_NativeXComponent_GetKeyEvent(component, &mut event) } != 0 || event.is_null() { + return; + } + let mut action = OH_NativeXComponent_KeyAction(-1); + let mut code = OH_NativeXComponent_KeyCode(-1); + let mut modifiers: u64 = 0; + unsafe { + OH_NativeXComponent_GetKeyEventAction(event, &mut action); + OH_NativeXComponent_GetKeyEventCode(event, &mut code); + OH_NativeXComponent_GetKeyEventModifierKeyStates(event, &mut modifiers); + } + let down = match action { + OH_NativeXComponent_KeyAction::OH_NATIVEXCOMPONENT_KEY_ACTION_DOWN => true, + OH_NativeXComponent_KeyAction::OH_NATIVEXCOMPONENT_KEY_ACTION_UP => false, + _ => return, + }; + send_from_ohos_message(FromOhosMessage::Key { down, code: code.0, modifiers }); +} + +#[no_mangle] +extern "C" fn on_dispatch_hover_event_cb(_component: *mut OH_NativeXComponent, _is_hover: bool) {} + #[no_mangle] extern "C" fn on_vsync_cb(_timestamp: ::core::ffi::c_longlong, data: *mut c_void) { // This beat is delivered; the main loop asks for the next one only if @@ -269,7 +371,12 @@ pub fn register_xcomponent_callbacks(env: &Env, xcomponent: &JsObject) { assert!(res == 0); } crate::log!("Got native_xcomponent!"); - let cbs = Box::new(OH_NativeXComponent_Callback { + register_native_xcomponent_callbacks(native_xcomponent); +} + +/// Surface, touch, mouse and key callbacks on makepad's XComponent, however +/// it was created (an ArkTS `XComponent`, or natively by `mount_content`). +pub fn register_native_xcomponent_callbacks(native_xcomponent: *mut OH_NativeXComponent) { let cbs = Box::new(OH_NativeXComponent_Callback { OnSurfaceCreated: Some(on_surface_created_cb), OnSurfaceChanged: Some(on_surface_changed_cb), OnSurfaceDestroyed: Some(on_surface_destroyed_cb), @@ -283,6 +390,20 @@ pub fn register_xcomponent_callbacks(env: &Env, xcomponent: &JsObject) { } else { crate::log!("Register XComponent callbacks successfully"); } + let mouse_cbs = Box::new(OH_NativeXComponent_MouseEvent_Callback { + DispatchMouseEvent: Some(on_dispatch_mouse_event_cb), + DispatchHoverEvent: Some(on_dispatch_hover_event_cb), + }); + let res = unsafe { + OH_NativeXComponent_RegisterMouseEventCallback(native_xcomponent, Box::leak(mouse_cbs) as *mut _) + }; + if res != 0 { + crate::error!("Failed to register XComponent mouse callbacks"); + } + let res = unsafe { OH_NativeXComponent_RegisterKeyEventCallback(native_xcomponent, Some(on_dispatch_key_event_cb)) }; + if res != 0 { + crate::error!("Failed to register XComponent key callback: {res}"); + } } pub fn register_vsync_callback(from_ohos_tx: mpsc::Sender) { @@ -313,6 +434,13 @@ pub fn debug_jsobject(obj: &JsObject, obj_name: &str) -> napi_ohos::Result<()> { Ok(()) } +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum MouseAction { + Down, + Up, + Move, +} + #[derive(Debug)] pub enum FromOhosMessage { Init { @@ -342,6 +470,45 @@ pub enum FromOhosMessage { /// response, a finished task): dispatch it; paint only on a VSync. Wake, Touch(Vec), + Key { + down: bool, + code: i32, + modifiers: u64, + }, + /// A system browser's document navigated (oh_web.rs). + WebNavigation { + id: u64, + url: String, + title: String, + loading: bool, + }, + WebPageError { + id: u64, + code: i32, + description: String, + url: String, + }, + /// A page called `octos_native.invoke`. + WebInvoke { + id: u64, + call_id: i64, + tool: String, + args: String, + }, + ImeKey { + code: i32, + modifiers: u64, + }, + Scroll { + scroll: Vec2d, + is_mouse: bool, + }, + Mouse { + action: MouseAction, + button: u32, + abs: Vec2d, + time: f64, + }, TextInput(TextInputEvent), DeleteLeft(i32), ResizeTextIME(bool, i32), diff --git a/platform/src/os/linux/open_harmony/oh_env.rs b/platform/src/os/linux/open_harmony/oh_env.rs new file mode 100644 index 0000000000..f06f5f06e6 --- /dev/null +++ b/platform/src/os/linux/open_harmony/oh_env.rs @@ -0,0 +1,67 @@ +//! What the app needs to know about its device and itself, from the NDK +//! instead of ArkTS: device type and OS name (`deviceinfo`) and display +//! density (`display_manager`). The application-level files, cache and temp +//! directories (`ability_runtime`, API 16) are only a fallback: the app's +//! data lives in its module's directories, which only ArkTS reports. + +use std::ffi::{c_char, c_int, CStr}; + +#[link(name = "deviceinfo_ndk.z")] +extern "C" { + fn OH_GetDeviceType() -> *const c_char; + fn OH_GetOSFullName() -> *const c_char; +} + +#[link(name = "native_display_manager")] +extern "C" { + fn OH_NativeDisplayManager_GetDefaultDisplayDensityPixels(density: *mut f32) -> c_int; +} + +#[link(name = "ability_runtime")] +extern "C" { + fn OH_AbilityRuntime_ApplicationContextGetFilesDir(buffer: *mut c_char, size: i32, written: *mut i32) -> c_int; + fn OH_AbilityRuntime_ApplicationContextGetCacheDir(buffer: *mut c_char, size: i32, written: *mut i32) -> c_int; + fn OH_AbilityRuntime_ApplicationContextGetTempDir(buffer: *mut c_char, size: i32, written: *mut i32) -> c_int; +} + +fn c_string(ptr: *const c_char) -> Option { + (!ptr.is_null()).then(|| unsafe { CStr::from_ptr(ptr) }.to_string_lossy().into_owned()).filter(|s| !s.is_empty()) +} + +/// "phone", "tablet", "2in1", ... +pub fn device_type() -> String { + c_string(unsafe { OH_GetDeviceType() }).unwrap_or_else(|| "phone".into()) +} + +pub fn os_full_name() -> String { + c_string(unsafe { OH_GetOSFullName() }).unwrap_or_else(|| "OpenHarmony".into()) +} + +/// Physical pixels per vp (densityDPI / 160). +pub fn display_density() -> f64 { + let mut density = 0.0f32; + let ok = unsafe { OH_NativeDisplayManager_GetDefaultDisplayDensityPixels(&mut density) } == 0; + if ok && density > 0.0 { density as f64 } else { 3.25 } +} + +fn context_dir(get: unsafe extern "C" fn(*mut c_char, i32, *mut i32) -> c_int) -> String { + let mut buffer = [0 as c_char; 1024]; + let mut written = 0i32; + if unsafe { get(buffer.as_mut_ptr(), buffer.len() as i32, &mut written) } != 0 { + crate::error!("ohos: application context directory unavailable"); + return String::new(); + } + c_string(buffer.as_ptr()).unwrap_or_default() +} + +pub fn files_dir() -> String { + context_dir(OH_AbilityRuntime_ApplicationContextGetFilesDir) +} + +pub fn cache_dir() -> String { + context_dir(OH_AbilityRuntime_ApplicationContextGetCacheDir) +} + +pub fn temp_dir() -> String { + context_dir(OH_AbilityRuntime_ApplicationContextGetTempDir) +} diff --git a/platform/src/os/linux/open_harmony/oh_ime.rs b/platform/src/os/linux/open_harmony/oh_ime.rs new file mode 100644 index 0000000000..37fab10f96 --- /dev/null +++ b/platform/src/os/linux/open_harmony/oh_ime.rs @@ -0,0 +1,293 @@ +//! The input method through the NDK (`libohinputmethod`, API 12): no ArkTS. +//! +//! One text-editor proxy for the app; the input method's editing messages +//! arrive on its callbacks and become the same messages touch, keys and text +//! already use. Attach, show and hide run on the ArkTS main thread, in the +//! order makepad asks for them, so a hide can never be overtaken by an older +//! show (what `phone/ohos/keyboard.patch` used to serialize in ArkTS). +#![allow(non_camel_case_types)] + +use super::oh_callbacks::{send_from_ohos_message, FromOhosMessage}; +use super::oh_sys::{uv_loop_t, uv_queue_work, uv_work_t}; +use crate::event::TextInputEvent; +use std::ffi::c_void; +use std::os::raw::c_int; +use std::sync::Mutex; + +#[repr(C)] +pub struct InputMethod_TextEditorProxy { + _unused: [u8; 0], +} +#[repr(C)] +pub struct InputMethod_InputMethodProxy { + _unused: [u8; 0], +} +#[repr(C)] +pub struct InputMethod_TextConfig { + _unused: [u8; 0], +} +#[repr(C)] +pub struct InputMethod_AttachOptions { + _unused: [u8; 0], +} +#[repr(C)] +pub struct InputMethod_PrivateCommand { + _unused: [u8; 0], +} + +type Proxy = *mut InputMethod_TextEditorProxy; +const IME_ERR_OK: c_int = 0; +const IME_KEYBOARD_STATUS_HIDE: c_int = 1; +const IME_TEXT_INPUT_TYPE_TEXT: c_int = 0; +const IME_ENTER_KEY_NONE: c_int = 1; + +#[link(name = "ohinputmethod")] +extern "C" { + fn OH_TextEditorProxy_Create() -> Proxy; + fn OH_TextEditorProxy_SetGetTextConfigFunc(p: Proxy, f: unsafe extern "C" fn(Proxy, *mut InputMethod_TextConfig)) -> c_int; + fn OH_TextEditorProxy_SetInsertTextFunc(p: Proxy, f: unsafe extern "C" fn(Proxy, *const u16, usize)) -> c_int; + fn OH_TextEditorProxy_SetDeleteForwardFunc(p: Proxy, f: unsafe extern "C" fn(Proxy, i32)) -> c_int; + fn OH_TextEditorProxy_SetDeleteBackwardFunc(p: Proxy, f: unsafe extern "C" fn(Proxy, i32)) -> c_int; + fn OH_TextEditorProxy_SetSendKeyboardStatusFunc(p: Proxy, f: unsafe extern "C" fn(Proxy, c_int)) -> c_int; + fn OH_TextEditorProxy_SetSendEnterKeyFunc(p: Proxy, f: unsafe extern "C" fn(Proxy, c_int)) -> c_int; + fn OH_TextEditorProxy_SetMoveCursorFunc(p: Proxy, f: unsafe extern "C" fn(Proxy, c_int)) -> c_int; + fn OH_TextEditorProxy_SetHandleSetSelectionFunc(p: Proxy, f: unsafe extern "C" fn(Proxy, i32, i32)) -> c_int; + fn OH_TextEditorProxy_SetHandleExtendActionFunc(p: Proxy, f: unsafe extern "C" fn(Proxy, c_int)) -> c_int; + fn OH_TextEditorProxy_SetGetLeftTextOfCursorFunc(p: Proxy, f: unsafe extern "C" fn(Proxy, i32, *mut u16, *mut usize)) -> c_int; + fn OH_TextEditorProxy_SetGetRightTextOfCursorFunc(p: Proxy, f: unsafe extern "C" fn(Proxy, i32, *mut u16, *mut usize)) -> c_int; + fn OH_TextEditorProxy_SetGetTextIndexAtCursorFunc(p: Proxy, f: unsafe extern "C" fn(Proxy) -> i32) -> c_int; + fn OH_TextEditorProxy_SetReceivePrivateCommandFunc( + p: Proxy, + f: unsafe extern "C" fn(Proxy, *mut *mut InputMethod_PrivateCommand, usize) -> i32, + ) -> c_int; + fn OH_TextEditorProxy_SetSetPreviewTextFunc(p: Proxy, f: unsafe extern "C" fn(Proxy, *const u16, usize, i32, i32) -> i32) -> c_int; + fn OH_TextEditorProxy_SetFinishTextPreviewFunc(p: Proxy, f: unsafe extern "C" fn(Proxy)) -> c_int; + fn OH_TextConfig_SetInputType(config: *mut InputMethod_TextConfig, input_type: c_int) -> c_int; + fn OH_TextConfig_SetEnterKeyType(config: *mut InputMethod_TextConfig, enter_key_type: c_int) -> c_int; + fn OH_AttachOptions_Create(show_keyboard: bool) -> *mut InputMethod_AttachOptions; + fn OH_AttachOptions_Destroy(options: *mut InputMethod_AttachOptions); + fn OH_InputMethodController_Attach( + editor: Proxy, + options: *mut InputMethod_AttachOptions, + ime: *mut *mut InputMethod_InputMethodProxy, + ) -> c_int; + /// API 23. + fn OH_InputMethodController_AttachWithUIContext( + context: *mut c_void, + editor: Proxy, + options: *mut InputMethod_AttachOptions, + ime: *mut *mut InputMethod_InputMethodProxy, + ) -> c_int; + fn OH_InputMethodController_Detach(ime: *mut InputMethod_InputMethodProxy) -> c_int; + fn OH_InputMethodProxy_ShowKeyboard(ime: *mut InputMethod_InputMethodProxy) -> c_int; + fn OH_InputMethodProxy_HideKeyboard(ime: *mut InputMethod_InputMethodProxy) -> c_int; +} + +struct Ime { + editor: Proxy, + ime: *mut InputMethod_InputMethodProxy, + uv_loop: *mut uv_loop_t, + /// The XComponent node's UI context (`ArkUI_ContextHandle`): attaching + /// needs it to know which window is editing. + context: *mut c_void, +} +// Touched only on the ArkTS main thread (via `on_main`), behind the mutex. +unsafe impl Send for Ime {} + +static IME: Mutex> = Mutex::new(None); + +/// Called once at startup with the main thread's event loop. +pub fn init(uv_loop: *mut uv_loop_t) { + let editor = unsafe { OH_TextEditorProxy_Create() }; + if editor.is_null() { + crate::error!("ime: OH_TextEditorProxy_Create failed"); + return; + } + unsafe { + OH_TextEditorProxy_SetGetTextConfigFunc(editor, get_text_config); + OH_TextEditorProxy_SetInsertTextFunc(editor, insert_text); + OH_TextEditorProxy_SetDeleteForwardFunc(editor, delete_forward); + OH_TextEditorProxy_SetDeleteBackwardFunc(editor, delete_backward); + OH_TextEditorProxy_SetSendKeyboardStatusFunc(editor, send_keyboard_status); + OH_TextEditorProxy_SetSendEnterKeyFunc(editor, send_enter_key); + OH_TextEditorProxy_SetMoveCursorFunc(editor, move_cursor); + OH_TextEditorProxy_SetHandleSetSelectionFunc(editor, set_selection); + OH_TextEditorProxy_SetHandleExtendActionFunc(editor, extend_action); + OH_TextEditorProxy_SetGetLeftTextOfCursorFunc(editor, no_text); + OH_TextEditorProxy_SetGetRightTextOfCursorFunc(editor, no_text); + OH_TextEditorProxy_SetGetTextIndexAtCursorFunc(editor, text_index); + OH_TextEditorProxy_SetReceivePrivateCommandFunc(editor, private_command); + OH_TextEditorProxy_SetSetPreviewTextFunc(editor, preview_text); + OH_TextEditorProxy_SetFinishTextPreviewFunc(editor, finish_preview); + } + *IME.lock().unwrap() = Some(Ime { editor, ime: std::ptr::null_mut(), uv_loop, context: std::ptr::null_mut() }); +} + +/// The UI context of makepad's XComponent node, once it is mounted. +pub fn set_context(context: *mut c_void) { + if let Some(ime) = IME.lock().unwrap().as_mut() { + ime.context = context; + } +} + +/// Show the keyboard: attach the editor the first time, then show. +pub fn show() { + on_main(|| { + let mut guard = IME.lock().unwrap(); + let Some(ime) = guard.as_mut() else { return }; + if ime.ime.is_null() { + let options = unsafe { OH_AttachOptions_Create(true) }; + let res = unsafe { + if ime.context.is_null() { + OH_InputMethodController_Attach(ime.editor, options, &mut ime.ime) + } else { + OH_InputMethodController_AttachWithUIContext(ime.context, ime.editor, options, &mut ime.ime) + } + }; + unsafe { OH_AttachOptions_Destroy(options) }; + if res != IME_ERR_OK { + crate::error!("ime: attach failed: {res}"); + ime.ime = std::ptr::null_mut(); + } + } else { + let res = unsafe { OH_InputMethodProxy_ShowKeyboard(ime.ime) }; + if res != IME_ERR_OK { + crate::error!("ime: show failed: {res}"); + } + } + }); +} + +/// Hide the keyboard and detach, so hardware keys stop going to the input +/// method while nothing is being edited. +pub fn hide() { + on_main(|| { + let mut guard = IME.lock().unwrap(); + let Some(ime) = guard.as_mut() else { return }; + if !ime.ime.is_null() { + unsafe { + OH_InputMethodProxy_HideKeyboard(ime.ime); + OH_InputMethodController_Detach(ime.ime); + } + ime.ime = std::ptr::null_mut(); + } + }); +} + +/// Run `f` on the ArkTS main thread, after everything queued before it. +/// Run `f` on the ArkTS main thread, after everything queued before it +/// (ArkWeb and the input method both need it). +pub fn run_on_main(f: impl FnOnce() + Send + 'static) { + on_main(f) +} + +fn on_main(f: impl FnOnce() + Send + 'static) { + let uv_loop = match IME.lock().unwrap().as_ref() { + Some(ime) => ime.uv_loop, + None => return, + }; + extern "C" fn work(_req: *mut uv_work_t) {} + extern "C" fn after(req: *mut uv_work_t, _status: c_int) { + let req = unsafe { Box::from_raw(req) }; + let f = unsafe { Box::from_raw(req.data as *mut Box) }; + f(); + } + let f: Box> = Box::new(Box::new(f)); + let mut req: Box = Box::new(unsafe { std::mem::zeroed() }); + req.data = Box::into_raw(f) as *mut c_void; + let req = Box::into_raw(req); + if unsafe { uv_queue_work(uv_loop, req, Some(work), Some(after)) } != 0 { + crate::error!("ime: uv_queue_work failed"); + unsafe { + drop(Box::from_raw((*req).data as *mut Box)); + drop(Box::from_raw(req)); + } + } +} + +// ---- editor callbacks: OpenHarmony key codes as in `oh_key_code` ---- + +unsafe extern "C" fn get_text_config(_p: Proxy, config: *mut InputMethod_TextConfig) { + OH_TextConfig_SetInputType(config, IME_TEXT_INPUT_TYPE_TEXT); + OH_TextConfig_SetEnterKeyType(config, IME_ENTER_KEY_NONE); +} + +unsafe extern "C" fn insert_text(_p: Proxy, text: *const u16, length: usize) { + if text.is_null() || length == 0 { + return; + } + let input = String::from_utf16_lossy(std::slice::from_raw_parts(text, length)); + send_from_ohos_message(FromOhosMessage::TextInput(TextInputEvent { + input, + replace_last: false, + was_paste: false, + ..Default::default() + })); +} + +unsafe extern "C" fn delete_backward(_p: Proxy, length: i32) { + send_from_ohos_message(FromOhosMessage::DeleteLeft(length)); +} + +unsafe extern "C" fn delete_forward(_p: Proxy, length: i32) { + for _ in 0..length.max(0) { + send_from_ohos_message(FromOhosMessage::ImeKey { code: 2071, modifiers: 0 }); + } +} + +unsafe extern "C" fn send_keyboard_status(_p: Proxy, status: c_int) { + if status == IME_KEYBOARD_STATUS_HIDE { + send_from_ohos_message(FromOhosMessage::ResizeTextIME(false, 0)); + } +} + +unsafe extern "C" fn send_enter_key(_p: Proxy, _enter_key_type: c_int) { + send_from_ohos_message(FromOhosMessage::ImeKey { code: 2054, modifiers: 0 }); +} + +unsafe extern "C" fn move_cursor(_p: Proxy, direction: c_int) { + // IME_DIRECTION_UP 1, DOWN 2, LEFT 3, RIGHT 4 + let code = match direction { + 1 => 2012, + 2 => 2013, + 3 => 2014, + 4 => 2015, + _ => return, + }; + send_from_ohos_message(FromOhosMessage::ImeKey { code, modifiers: 0 }); +} + +unsafe extern "C" fn extend_action(_p: Proxy, action: c_int) { + // SELECT_ALL 0, CUT 3, COPY 4, PASTE 5: Ctrl+A, X, C, V + let code = match action { + 0 => 2017, + 3 => 2040, + 4 => 2019, + 5 => 2038, + _ => return, + }; + send_from_ohos_message(FromOhosMessage::ImeKey { code, modifiers: 1 }); +} + +unsafe extern "C" fn set_selection(_p: Proxy, _start: i32, _end: i32) {} + +/// makepad's editors keep their own text; the input method sees none of it. +unsafe extern "C" fn no_text(_p: Proxy, _number: i32, _text: *mut u16, length: *mut usize) { + if !length.is_null() { + *length = 0; + } +} + +unsafe extern "C" fn text_index(_p: Proxy) -> i32 { + 0 +} + +unsafe extern "C" fn private_command(_p: Proxy, _commands: *mut *mut InputMethod_PrivateCommand, _size: usize) -> i32 { + 0 +} + +unsafe extern "C" fn preview_text(_p: Proxy, _text: *const u16, _length: usize, _start: i32, _end: i32) -> i32 { + 0 +} + +unsafe extern "C" fn finish_preview(_p: Proxy) {} diff --git a/platform/src/os/linux/open_harmony/oh_web.rs b/platform/src/os/linux/open_harmony/oh_web.rs new file mode 100644 index 0000000000..fa556b2788 --- /dev/null +++ b/platform/src/os/linux/open_harmony/oh_web.rs @@ -0,0 +1,303 @@ +//! makepad's system browsers (`cx.system_browser(id)`) on OpenHarmony, with +//! ArkWeb. +//! +//! The NDK has no web node, so ArkTS declares one `Web` per browser, on top of +//! makepad's XComponent, from the list kept here; everything else stays out of +//! ArkTS where ArkWeb's NDK allows it (`web_shim.cpp`): +//! +//! - the page's `window.octos_native.invoke(callId, tool, args)` is a native +//! proxy, delivered here as `NativeSystemBrowserInvoke`; +//! - `eval_js` (replies, events) is a native `runJavaScript`. +//! +//! What the NDK lacks stays a thin ArkTS step: loading a URL or HTML and +//! history are controller calls ArkTS takes from `web_view_take_commands`, and +//! navigation, errors and the navigation policy are `Web` events it forwards. +//! +//! Each browser's controller is named with its tag, `makepad_web_`. + +use super::oh_callbacks::{send_from_ohos_message, FromOhosMessage}; +use crate::makepad_math::Rect; +use napi_derive_ohos::napi; +use std::collections::VecDeque; +use std::ffi::{c_char, CString}; +use std::sync::Mutex; + +extern "C" { + fn makepad_arkweb_register_bridge(tag: *const c_char) -> i32; + fn makepad_arkweb_run_js(tag: *const c_char, js: *const c_char, len: usize) -> i32; +} + +/// What a browser wants ArkTS to do with its controller. +enum Command { + Url(String), + Html { html: String, base_url: String }, + HistoryGo(i32), +} + +struct Browser { + id: u64, + navigable: bool, + /// Logical points (vp), relative to the page. + rect: Rect, + visible: bool, + /// The bridge is registered; the queued loads may run. + attached: bool, + /// URLs we asked to load: navigation to these is never blocked. + requested: Vec, +} + +#[derive(Default)] +struct State { + browsers: Vec, + commands: VecDeque<(u64, Command)>, +} + +static STATE: Mutex = Mutex::new(State { browsers: Vec::new(), commands: VecDeque::new() }); + +pub fn tag(id: u64) -> String { + format!("makepad_web_{id}") +} + +fn id_of(tag: &str) -> Option { + tag.strip_prefix("makepad_web_")?.parse().ok() +} + +fn with_browser(id: u64, f: impl FnOnce(&mut Browser) -> R) -> Option { + STATE.lock().unwrap().browsers.iter_mut().find(|b| b.id == id).map(f) +} + +// ---- Cx side (from CxOsOp) ---- + +/// Spawn (or reuse) a browser showing `url`. Returns whether ArkTS must +/// re-read the list. +pub fn spawn(id: u64, url: &str, navigable: bool) -> bool { + let mut state = STATE.lock().unwrap(); + let new = !state.browsers.iter().any(|b| b.id == id); + if new { + state.browsers.push(Browser { + id, + navigable, + rect: Rect::default(), + visible: false, + attached: false, + requested: Vec::new(), + }); + } + if let Some(b) = state.browsers.iter_mut().find(|b| b.id == id) { + b.navigable = navigable; + if !url.is_empty() && url != "about:blank" { + b.requested.push(url.to_string()); + } + } + if !url.is_empty() && url != "about:blank" { + state.commands.push_back((id, Command::Url(url.to_string()))); + } + true +} + +/// Place the browser (logical points) and show or hide it. Returns whether +/// anything changed. +pub fn update(id: u64, rect: Rect, visible: bool) -> bool { + with_browser(id, |b| { + let changed = b.rect != rect || b.visible != visible; + b.rect = rect; + b.visible = visible; + changed + }) + .unwrap_or(false) +} + +pub fn hide(id: u64) -> bool { + with_browser(id, |b| std::mem::replace(&mut b.visible, false)).unwrap_or(false) +} + +pub fn close(id: u64) -> bool { + let mut state = STATE.lock().unwrap(); + let before = state.browsers.len(); + state.browsers.retain(|b| b.id != id); + state.commands.retain(|(c, _)| *c != id); + state.browsers.len() != before +} + +pub fn set_url(id: u64, url: &str) { + with_browser(id, |b| b.requested.push(url.to_string())); + STATE.lock().unwrap().commands.push_back((id, Command::Url(url.to_string()))); +} + +pub fn set_html(id: u64, html: &str, base_url: &str) { + let base_url = if base_url.is_empty() { "https://octos-one.app/" } else { base_url }; + with_browser(id, |b| b.requested.push(base_url.to_string())); + STATE + .lock() + .unwrap() + .commands + .push_back((id, Command::Html { html: html.to_string(), base_url: base_url.to_string() })); +} + +pub fn history_go(id: u64, delta: i32) { + STATE.lock().unwrap().commands.push_back((id, Command::HistoryGo(delta))); +} + +/// Evaluate `js` in the browser's document, natively. Runs on the ArkTS main +/// thread (ArkWeb's requirement). +pub fn eval_js(id: u64, js: &str) { + let tag = tag(id); + let js = js.to_string(); + super::oh_ime::run_on_main(move || { + let Ok(tag) = CString::new(tag) else { return }; + if unsafe { makepad_arkweb_run_js(tag.as_ptr(), js.as_ptr() as *const c_char, js.len()) } != 0 { + crate::error!("web: runJavaScript unavailable"); + } + }); +} + +/// Whether loads or history steps are waiting for an attached browser (one +/// still attaching flushes its own when it attaches). +pub fn has_commands() -> bool { + let state = STATE.lock().unwrap(); + state.commands.iter().any(|(id, _)| state.browsers.iter().any(|b| b.id == *id && b.attached)) +} + +// ---- ArkTS side (napi, on the ArkTS main thread) ---- + +/// The browsers to declare: `tag|x|y|w|h|visible`, in vp. +#[napi] +pub fn web_view_list() -> Vec { + STATE + .lock() + .unwrap() + .browsers + .iter() + .map(|b| { + format!( + "{}|{}|{}|{}|{}|{}", + tag(b.id), + b.rect.pos.x, + b.rect.pos.y, + b.rect.size.x.max(1.0), + b.rect.size.y.max(1.0), + b.visible as u8 + ) + }) + .collect() +} + +/// The controller calls that are due, for attached browsers only: +/// `tag|url|`, `tag|html||`, `tag|history|`. +#[napi] +pub fn web_view_take_commands() -> Vec { + let mut state = STATE.lock().unwrap(); + let attached: Vec = state.browsers.iter().filter(|b| b.attached).map(|b| b.id).collect(); + let mut out = Vec::new(); + let mut keep = VecDeque::new(); + while let Some((id, command)) = state.commands.pop_front() { + if !attached.contains(&id) { + keep.push_back((id, command)); + continue; + } + out.push(match command { + Command::Url(url) => format!("{}|url|{}", tag(id), url), + Command::Html { html, base_url } => format!("{}|html|{}|{}", tag(id), base_url, html), + Command::HistoryGo(delta) => format!("{}|history|{}", tag(id), delta), + }); + } + state.commands = keep; + out +} + +/// The browser's controller is attached: expose `octos_native` to the +/// documents it loads from now on (natively). +#[napi] +pub fn web_view_attached(tag: String) { + if let Ok(c_tag) = CString::new(tag) { + if unsafe { makepad_arkweb_register_bridge(c_tag.as_ptr()) } != 0 { + crate::error!("web: registerJavaScriptProxy unavailable; octos.invoke will not reach the app"); + } + } +} + +/// The browser finished its first (blank) page: its queued loads may run. +/// Loading earlier races that initial page, which aborts the load. +#[napi] +pub fn web_view_ready(tag: String) { + let Some(id) = id_of(&tag) else { return }; + with_browser(id, |b| b.attached = true); +} + +/// The navigation policy: a browser opened with `spawn` stays on the documents +/// the app loads; one opened with `spawn_navigable` follows links, scripts and +/// redirects. +/// +/// A `spawn` browser may also move to another host of the same site as a page +/// the app asked for: ArkWeb on a 2-in-1 presents a desktop browser, so a +/// mobile host redirects (m.youtube.com -> www.youtube.com) where Android's +/// mobile WebView would not, and that hop is still the document the app +/// opened, not a link the person followed elsewhere. +#[napi] +pub fn web_view_may_navigate(tag: String, url: String) -> bool { + let Some(id) = id_of(&tag) else { return true }; + with_browser(id, |b| { + b.navigable + || url.starts_with("about:") + || url.starts_with("data:") + || b.requested.iter().any(|r| { + url == *r || url.trim_end_matches('/') == r.trim_end_matches('/') || same_site(&url, r) + }) + }) + .unwrap_or(true) +} + +/// Whether two https URLs are on the same site: the last two labels of the +/// host (youtube.com), enough for the mobile/desktop host split it serves. +fn same_site(a: &str, b: &str) -> bool { + fn site(url: &str) -> Option { + let rest = url.strip_prefix("https://")?; + let host = rest.split(['/', '?', '#', ':']).next()?.to_ascii_lowercase(); + let labels: Vec<&str> = host.split('.').filter(|l| !l.is_empty()).collect(); + (labels.len() >= 2).then(|| labels[labels.len() - 2..].join(".")) + } + matches!((site(a), site(b)), (Some(x), Some(y)) if x == y) +} + +#[napi] +pub fn web_view_navigation(tag: String, url: String, title: String, loading: bool) { + let Some(id) = id_of(&tag) else { return }; + send_from_ohos_message(FromOhosMessage::WebNavigation { id, url, title, loading }); +} + +/// A main-frame load failed. +#[napi] +pub fn web_view_page_error(tag: String, code: i32, description: String, url: String) { + let Some(id) = id_of(&tag) else { return }; + send_from_ohos_message(FromOhosMessage::WebPageError { id, code, description, url }); +} + +/// `octos_native.invoke(callId, tool, args)` from a page (web_shim.cpp). +#[no_mangle] +extern "C" fn makepad_ohos_web_invoke( + tag: *const c_char, + call_id: *const c_char, + call_id_len: usize, + tool: *const c_char, + tool_len: usize, + args: *const c_char, + args_len: usize, +) { + let text = |p: *const c_char, n: usize| { + if p.is_null() { + String::new() + } else { + let bytes = unsafe { std::slice::from_raw_parts(p as *const u8, n) }; + String::from_utf8_lossy(bytes).trim_end_matches('\0').to_string() + } + }; + let tag = if tag.is_null() { String::new() } else { unsafe { std::ffi::CStr::from_ptr(tag) }.to_string_lossy().into_owned() }; + let Some(id) = id_of(&tag) else { return }; + let call_id = text(call_id, call_id_len).trim().parse::().map(|v| v as i64).unwrap_or(0); + send_from_ohos_message(FromOhosMessage::WebInvoke { + id, + call_id, + tool: text(tool, tool_len), + args: text(args, args_len), + }); +} diff --git a/platform/src/os/linux/open_harmony/open_harmony.rs b/platform/src/os/linux/open_harmony/open_harmony.rs index 080efb55ab..21bfaedf20 100644 --- a/platform/src/os/linux/open_harmony/open_harmony.rs +++ b/platform/src/os/linux/open_harmony/open_harmony.rs @@ -16,11 +16,12 @@ use { draw_pass::{CxDrawPassParent, DrawPassClearColor, DrawPassClearDepth, DrawPassId}, egl_sys::{self, LibEgl, EGL_NONE}, event::{ - Event, KeyCode, KeyEvent, SafeAreaInsets, TouchUpdateEvent, VirtualKeyboardEvent, - WindowGeom, + Event, KeyCode, KeyEvent, KeyModifiers, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, ScrollEvent, + SafeAreaInsets, TouchUpdateEvent, VirtualKeyboardEvent, WindowGeom, }, gpu_info::GpuPerformance, - makepad_live_id::LiveId, + area::Area, + makepad_live_id::{live_id, LiveId}, makepad_math::*, os::cx_native::EventFlow, shared_framebuf::{PollTimer, PollTimers}, @@ -30,7 +31,7 @@ use { }, napi_derive_ohos::napi, napi_ohos::{sys::*, Env, JsObject, NapiRaw}, - std::{ffi::CString, os::raw::c_void, ptr::null_mut, rc::Rc, sync::mpsc, time::Instant}, + std::{cell::Cell, ffi::CString, os::raw::c_void, ptr::null_mut, rc::Rc, sync::mpsc, time::Instant}, }; #[napi(js_name = "onCreate")] @@ -43,16 +44,16 @@ pub fn ohos_ability_on_create(env: Env, ark_ts: JsObject) -> napi_ohos::Result<( assert!(status == 0); let arkts_obj = ArkTsObjRef::new(raw_env, arkts_ref); - let device_type = arkts_obj - .get_string("deviceType") - .unwrap_or("phone".to_string()); - let os_full_name = arkts_obj - .get_string("osFullName") - .unwrap_or("OpenHarmony".to_string()); - let display_density = arkts_obj.get_number("displayDensity").unwrap_or(3.25); - let files_dir = arkts_obj.get_string("filesDir").unwrap(); - let cache_dir = arkts_obj.get_string("cacheDir").unwrap(); - let temp_dir = arkts_obj.get_string("tempDir").unwrap(); + // Device and display come from the NDK (oh_env.rs). The directories + // are the module's (`haps//files`), which only ArkTS's context + // reports: the NDK has the application's, a different place, and the + // app's data must not move. The resource manager is an ArkTS object. + let device_type = super::oh_env::device_type(); + let os_full_name = super::oh_env::os_full_name(); + let display_density = super::oh_env::display_density(); + let files_dir = arkts_obj.get_string("filesDir").unwrap_or_else(|_| super::oh_env::files_dir()); + let cache_dir = arkts_obj.get_string("cacheDir").unwrap_or_else(|_| super::oh_env::cache_dir()); + let temp_dir = arkts_obj.get_string("tempDir").unwrap_or_else(|_| super::oh_env::temp_dir()); let res_mgr = arkts_obj.get_property("resMgr").unwrap(); // The entry ability collects the Want's `makepad.*` parameters into @@ -384,12 +385,109 @@ impl Cx { }; self.fingers.process_touch_update_end(&e.touches); } + FromOhosMessage::Key { down, code, modifiers } => { + let Some(key_code) = oh_key_code(code) else { return }; + let time = self.os.timers.time_now(); + // A press can reach us both as a key and as an input-method + // message (Backspace as deleteLeft, Enter as a function key): + // whichever comes second within 150 ms is the same press. + if down { + if key_code == KeyCode::Backspace && time - self.os.last_ime_backspace < 0.15 { return; } + if let Some((ime_code, ime_time)) = self.os.last_ime_key { + if ime_code == key_code && time - ime_time < 0.15 { return; } + } + if key_code == KeyCode::Backspace { self.os.last_key_backspace = time; } + self.os.last_native_key = Some((key_code, time)); + } + self.key_event(key_code, modifiers, down, time); + } + FromOhosMessage::ImeKey { code, modifiers } => { + let Some(key_code) = oh_key_code(code) else { return }; + let time = self.os.timers.time_now(); + if let Some((native_code, native_time)) = self.os.last_native_key { + if native_code == key_code && time - native_time < 0.15 { return; } + } + self.os.last_ime_key = Some((key_code, time)); + self.key_event(key_code, modifiers, true, time); + self.key_event(key_code, modifiers, false, time); + } + FromOhosMessage::WebNavigation { id, url, title, loading } => { + Cx::post_action(crate::event::NativeSystemBrowserNavigation { + browser_id: id, + url, + title, + loading, + error: None, + }); + self.handle_action_receiver(); + } + FromOhosMessage::WebPageError { id, code, description, url } => { + Cx::post_action(crate::event::NativeSystemBrowserPageError { + browser_id: id, + code, + description, + url, + }); + self.handle_action_receiver(); + } + FromOhosMessage::WebInvoke { id, call_id, tool, args } => { + Cx::post_action(crate::event::NativeSystemBrowserInvoke { browser_id: id, call_id, tool, args }); + self.handle_action_receiver(); + } + FromOhosMessage::Scroll { scroll, is_mouse } => { + let time = self.os.timers.time_now(); + self.call_event_handler(&Event::Scroll(ScrollEvent { + window_id: CxWindowPool::id_zero(), + scroll, + abs: self.os.last_mouse_abs, + modifiers: Default::default(), + handled_x: Cell::new(false), + handled_y: Cell::new(false), + is_mouse, + time, + phase: Default::default(), + })); + } + FromOhosMessage::Mouse { action, button, abs, time } => { + let window_id = CxWindowPool::id_zero(); + let dpi_factor = self.windows[window_id].dpi_override.unwrap_or(self.os.dpi_factor); + let abs = abs / dpi_factor; + self.os.last_mouse_abs = abs; + let button = MouseButton::from_bits_retain(button); + let modifiers = Default::default(); + match action { + MouseAction::Down => { + self.fingers.process_tap_count(abs, time); + self.fingers.mouse_down(button, window_id); + self.call_event_handler(&Event::MouseDown(MouseDownEvent { + abs, button, window_id, modifiers, time, handled: Cell::new(Area::Empty), + })); + } + MouseAction::Move => { + self.call_event_handler(&Event::MouseMove(MouseMoveEvent { + abs, lock_delta: Vec2d::default(), window_id, modifiers, time, + handled: Cell::new(Area::Empty), + })); + self.fingers.cycle_hover_area(live_id!(mouse).into()); + self.fingers.switch_captures(); + } + MouseAction::Up => { + self.call_event_handler(&Event::MouseUp(MouseUpEvent { + abs, button, window_id, modifiers, time, + })); + self.fingers.mouse_up(button); + self.fingers.cycle_hover_area(live_id!(mouse).into()); + } + } + } FromOhosMessage::TextInput(e) => { self.call_event_handler(&Event::TextInput(e)); } FromOhosMessage::DeleteLeft(length) => { for _ in 0..length { let time = self.os.timers.time_now(); + if time - self.os.last_key_backspace < 0.15 { continue; } + self.os.last_ime_backspace = time; let e = KeyEvent { key_code: KeyCode::Backspace, is_repeat: false, @@ -444,6 +542,9 @@ impl Cx { }) => { self.os.dpi_factor = display_density; self.os.raw_file = Some(raw_file); + // `HOME` (the person's shared files) is not the app's to write: + // its own files directory is, as on Android and iOS. + crate::home::set_platform_data_dir(std::path::Path::new(&files_dir)); self.os_type = OsType::OpenHarmony(OpenHarmonyParams { files_dir, cache_dir, @@ -453,6 +554,9 @@ impl Cx { display_density, }); self.os.arkts_obj = Some(ArkTsObjRef::new(raw_env, arkts_ref)); + if let Some(uv_loop) = super::oh_util::get_uv_loop(raw_env) { + super::oh_ime::init(uv_loop); + } return true; } Ok(other) => { @@ -473,7 +577,10 @@ impl Cx { // The Studio websocket connects between Init and the XComponent's // surface and posts a wake-up through this channel; a single recv // took that wake-up for the surface, handed EGL a null window and - // the app died in an assertion. Wait for the surface itself. + // the app died in an assertion. Wait for the surface itself. Other + // messages can come first too (the page reports its avoid area as it + // mounts the XComponent): keep them for after the surface exists. + let mut early = Vec::new(); loop { match from_ohos_rx.recv() { Ok(FromOhosMessage::SurfaceCreated { @@ -488,13 +595,17 @@ impl Cx { height, self.os.dpi_factor ); + for message in early { + send_from_ohos_message(message); + } return window; } Ok(other) => { crate::log!( - "message before SurfaceCreated skipped: {}", + "message before SurfaceCreated kept: {}", ohos_message_name(&other) ); + early.push(other); } Err(_) => { crate::error!("Can't recv SurfaceCreated from arkts"); @@ -755,6 +866,9 @@ impl Cx { } fn handle_platform_ops(&mut self) -> EventFlow { + // System browsers that appeared, moved, hid or closed this round: + // ArkTS re-reads the list once, after the loop. + let mut web_changed = false; while let Some(op) = self.platform_ops.pop_front() { //crate::log!("============ handle_platform_ops"); match op { @@ -820,21 +934,13 @@ impl Cx { } CxOsOp::ShowTextIME(_area, _pos, config) => { if self.os.last_ime_config.as_ref() != Some(&config) { - let _ = self.os.arkts_obj.as_mut().unwrap().call_js_function( - "showKeyBoard", - 0, - std::ptr::null_mut(), - ); + super::oh_ime::show(); self.os.last_ime_config = Some(config); } } CxOsOp::HideTextIME => { self.os.last_ime_config = None; - let _ = self.os.arkts_obj.as_mut().unwrap().call_js_function( - "hideKeyBoard", - 0, - std::ptr::null_mut(), - ); + super::oh_ime::hide(); //self.os.keyboard_visible = false; //unsafe {android_jni::to_java_show_keyboard(false);} } @@ -903,6 +1009,32 @@ impl Cx { VideoPlaybackResourcesReleasedEvent { video_id }, )); } + CxOsOp::SpawnSystemBrowser { browser_id, url, navigable } => { + web_changed |= super::oh_web::spawn(browser_id.0, &url, navigable); + } + CxOsOp::UpdateSystemBrowser { browser_id, area, visible } => { + // Logical points are ArkUI's vp: the page is the window. + let rect = area.clipped_rect(self); + web_changed |= super::oh_web::update(browser_id.0, rect, visible); + } + CxOsOp::DetachSystemBrowser { browser_id } => { + web_changed |= super::oh_web::hide(browser_id.0); + } + CxOsOp::CloseSystemBrowser { browser_id } => { + web_changed |= super::oh_web::close(browser_id.0); + } + CxOsOp::SetSystemBrowserUrl { browser_id, url, replace: _ } => { + super::oh_web::set_url(browser_id.0, &url); + } + CxOsOp::SetSystemBrowserHtml { browser_id, html, base_url } => { + super::oh_web::set_html(browser_id.0, &html, &base_url); + } + CxOsOp::SystemBrowserHistoryGo { browser_id, delta } => { + super::oh_web::history_go(browser_id.0, delta); + } + CxOsOp::EvalSystemBrowserJs { browser_id, js } => { + super::oh_web::eval_js(browser_id.0, &js); + } CxOsOp::AttachCameraNativePreview { .. } | CxOsOp::UpdateCameraNativePreview { .. } | CxOsOp::DetachCameraNativePreview { .. } => {} @@ -911,6 +1043,13 @@ impl Cx { } } } + if web_changed || super::oh_web::has_commands() { + // ArkTS declares the `Web` components and runs the loads ArkWeb's + // NDK cannot; it pulls both from oh_web.rs. + if let Some(arkts) = self.os.arkts_obj.as_mut() { + let _ = arkts.call_js_function("syncWebViews", 0, std::ptr::null_mut()); + } + } EventFlow::Poll } } @@ -943,6 +1082,15 @@ pub struct CxOhosDisplay { } pub struct CxOs { + /// When Backspace last arrived as a key and as the input method's + /// deleteLeft: one press can come both ways. + pub last_key_backspace: f64, + pub last_ime_backspace: f64, + /// The last key down from the XComponent and from the input method. + /// Where the mouse last was (logical points): scrolls go there. + pub last_mouse_abs: Vec2d, + pub last_native_key: Option<(KeyCode, f64)>, + pub last_ime_key: Option<(KeyCode, f64)>, pub first_after_resize: bool, pub display_size: Vec2d, pub dpi_factor: f64, @@ -1117,6 +1265,11 @@ impl CxOs { impl Default for CxOs { fn default() -> Self { Self { + last_key_backspace: f64::MIN, + last_ime_backspace: f64::MIN, + last_mouse_abs: Vec2d::default(), + last_native_key: None, + last_ime_key: None, first_after_resize: true, display_size: dvec2(1260 as f64, 2503 as f64), dpi_factor: 3.25, @@ -1190,6 +1343,85 @@ impl CxOhosDisplay { } } +impl Cx { + fn key_event(&mut self, key_code: KeyCode, modifiers: u64, down: bool, time: f64) { + let e = KeyEvent { + key_code, + is_repeat: false, + modifiers: KeyModifiers { + control: modifiers & 1 != 0, + shift: modifiers & 2 != 0, + alt: modifiers & 4 != 0, + logo: false, + }, + time, + }; + if down { + self.keyboard.process_key_down(e.clone()); + self.call_event_handler(&Event::KeyDown(e)); + } else { + self.keyboard.process_key_up(e.clone()); + self.call_event_handler(&Event::KeyUp(e)); + } + } +} + +/// An OpenHarmony `KeyCode` (OH_NativeXComponent_KeyCode) as makepad's. +fn oh_key_code(code: i32) -> Option { + const LETTERS: [KeyCode; 26] = [ + KeyCode::KeyA, KeyCode::KeyB, KeyCode::KeyC, KeyCode::KeyD, KeyCode::KeyE, KeyCode::KeyF, + KeyCode::KeyG, KeyCode::KeyH, KeyCode::KeyI, KeyCode::KeyJ, KeyCode::KeyK, KeyCode::KeyL, + KeyCode::KeyM, KeyCode::KeyN, KeyCode::KeyO, KeyCode::KeyP, KeyCode::KeyQ, KeyCode::KeyR, + KeyCode::KeyS, KeyCode::KeyT, KeyCode::KeyU, KeyCode::KeyV, KeyCode::KeyW, KeyCode::KeyX, + KeyCode::KeyY, KeyCode::KeyZ, + ]; + const DIGITS: [KeyCode; 10] = [ + KeyCode::Key0, KeyCode::Key1, KeyCode::Key2, KeyCode::Key3, KeyCode::Key4, + KeyCode::Key5, KeyCode::Key6, KeyCode::Key7, KeyCode::Key8, KeyCode::Key9, + ]; + const FKEYS: [KeyCode; 12] = [ + KeyCode::F1, KeyCode::F2, KeyCode::F3, KeyCode::F4, KeyCode::F5, KeyCode::F6, + KeyCode::F7, KeyCode::F8, KeyCode::F9, KeyCode::F10, KeyCode::F11, KeyCode::F12, + ]; + Some(match code { + 2000..=2009 => DIGITS[(code - 2000) as usize], + 2017..=2042 => LETTERS[(code - 2017) as usize], + 2090..=2101 => FKEYS[(code - 2090) as usize], + 2012 => KeyCode::ArrowUp, + 2013 => KeyCode::ArrowDown, + 2014 => KeyCode::ArrowLeft, + 2015 => KeyCode::ArrowRight, + 2043 => KeyCode::Comma, + 2044 => KeyCode::Period, + 2045 | 2046 => KeyCode::Alt, + 2047 | 2048 => KeyCode::Shift, + 2049 => KeyCode::Tab, + 2050 => KeyCode::Space, + 2054 => KeyCode::ReturnKey, + 2055 => KeyCode::Backspace, + 2056 => KeyCode::Backtick, + 2057 => KeyCode::Minus, + 2058 => KeyCode::Equals, + 2059 => KeyCode::LBracket, + 2060 => KeyCode::RBracket, + 2061 => KeyCode::Backslash, + 2062 => KeyCode::Semicolon, + 2063 => KeyCode::Quote, + 2064 => KeyCode::Slash, + 2068 => KeyCode::PageUp, + 2069 => KeyCode::PageDown, + 2070 => KeyCode::Escape, + 2071 => KeyCode::Delete, + 2072 | 2073 => KeyCode::Control, + 2074 => KeyCode::Capslock, + 2076 | 2077 => KeyCode::Logo, + 2081 => KeyCode::Home, + 2082 => KeyCode::End, + 2083 => KeyCode::Insert, + _ => return None, + }) +} + /// The `"key":"value"` pairs of a flat JSON object of strings, in order. /// Only `\"` and `\\` escapes are honoured: launch parameters are host /// names, build ids and paths, never structured text. @@ -1236,6 +1468,13 @@ fn ohos_message_name(message: &FromOhosMessage) -> &'static str { FromOhosMessage::VSync => "VSync", FromOhosMessage::Wake => "Wake", FromOhosMessage::Touch(_) => "Touch", + FromOhosMessage::Key { .. } => "Key", + FromOhosMessage::WebNavigation { .. } => "WebNavigation", + FromOhosMessage::WebPageError { .. } => "WebPageError", + FromOhosMessage::WebInvoke { .. } => "WebInvoke", + FromOhosMessage::ImeKey { .. } => "ImeKey", + FromOhosMessage::Scroll { .. } => "Scroll", + FromOhosMessage::Mouse { .. } => "Mouse", FromOhosMessage::TextInput(_) => "TextInput", FromOhosMessage::DeleteLeft(_) => "DeleteLeft", FromOhosMessage::ResizeTextIME(..) => "ResizeTextIME", diff --git a/platform/src/os/linux/open_harmony/web_shim.cpp b/platform/src/os/linux/open_harmony/web_shim.cpp new file mode 100644 index 0000000000..faac251427 --- /dev/null +++ b/platform/src/os/linux/open_harmony/web_shim.cpp @@ -0,0 +1,68 @@ +// makepad's system browsers on OpenHarmony: the parts of ArkWeb the NDK +// exposes, addressed by web tag. +// +// ArkTS has to declare each `Web` component (there is no web node in the NDK), +// and its controller is named with the browser's tag. From then on the page +// talks to Rust directly: `window.octos_native.invoke(callId, tool, args)` is a +// proxy registered here, and replies and events go back with runJavaScript. +// +// C++ for the same reason as node_shim.cpp: the ArkWeb APIs are structs of +// function pointers whose order is the ABI, checked here by the SDK header and +// by ARKWEB_MEMBER_EXISTS (a device may predate a member). +#include +#include + +#include +#include + +// Rust (oh_web.rs): one `invoke(callId, tool, args)` from a page. +extern "C" void makepad_ohos_web_invoke(const char *tag, const char *call_id, size_t call_id_len, const char *tool, + size_t tool_len, const char *args, size_t args_len); + +// Main thread only (oh_web.rs runs every call there), so a plain global: a +// function-local static would need the C++ runtime's guard functions, which +// the app does not link. +static ArkWeb_ControllerAPI *g_controller = nullptr; + +static ArkWeb_ControllerAPI *controller_api() { + if (g_controller == nullptr) { + g_controller = reinterpret_cast(OH_ArkWeb_GetNativeAPI(ARKWEB_NATIVE_CONTROLLER)); + } + return g_controller; +} + +static void on_invoke(const char *tag, const ArkWeb_JavaScriptBridgeData *data, size_t size, void *) { + if (size < 3 || data == nullptr) { + return; + } + makepad_ohos_web_invoke(tag, reinterpret_cast(data[0].buffer), data[0].size, + reinterpret_cast(data[1].buffer), data[1].size, + reinterpret_cast(data[2].buffer), data[2].size); +} + +static const ArkWeb_ProxyMethod kMethods[] = {{"invoke", on_invoke, nullptr}}; +static const ArkWeb_ProxyObject kBridge = {"octos_native", kMethods, 1}; + +// Expose `octos_native` to the documents `tag` loads from now on. Call once +// its controller is attached and before loading the first document. 0 = ok. +extern "C" int makepad_arkweb_register_bridge(const char *tag) { + ArkWeb_ControllerAPI *api = controller_api(); + if (api == nullptr || ARKWEB_MEMBER_MISSING(api, registerJavaScriptProxy)) { + return -1; + } + api->registerJavaScriptProxy(tag, &kBridge); + return 0; +} + +static void on_js_result(const char *, const ArkWeb_JavaScriptBridgeData *, void *) {} + +// Evaluate `js` (len bytes, not NUL-terminated) in the document `tag` shows. +extern "C" int makepad_arkweb_run_js(const char *tag, const char *js, size_t len) { + ArkWeb_ControllerAPI *api = controller_api(); + if (api == nullptr || ARKWEB_MEMBER_MISSING(api, runJavaScript)) { + return -1; + } + ArkWeb_JavaScriptObject object = {reinterpret_cast(js), len, on_js_result, nullptr}; + api->runJavaScript(tag, &object); + return 0; +} diff --git a/platform/src/os/windows/d3d11.rs b/platform/src/os/windows/d3d11.rs index d6d4d7fdb7..64c63b2938 100644 --- a/platform/src/os/windows/d3d11.rs +++ b/platform/src/os/windows/d3d11.rs @@ -29,11 +29,11 @@ use crate::{ PCWSTR, }, Win32::{ - Foundation::{CloseHandle, HANDLE, HMODULE, S_FALSE, WAIT_TIMEOUT}, + Foundation::{CloseHandle, HANDLE, HMODULE, S_FALSE, WAIT_OBJECT_0, WAIT_TIMEOUT}, Graphics::{ Direct3D::{ Fxc::D3DCompile, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST, - D3D_DRIVER_TYPE_UNKNOWN, D3D_FEATURE_LEVEL_11_0, D3D_SRV_DIMENSION, + D3D_DRIVER_TYPE_UNKNOWN, D3D_DRIVER_TYPE_WARP, D3D_FEATURE_LEVEL_11_0, D3D_SRV_DIMENSION, D3D_SRV_DIMENSION_TEXTURECUBE, }, Direct3D11::{ @@ -90,7 +90,7 @@ use crate::{ DXGI_FORMAT_R8_UNORM, DXGI_SAMPLE_DESC, }, - CreateDXGIFactory2, IDXGIFactory2, IDXGIKeyedMutex, IDXGIResource1, + CreateDXGIFactory2, IDXGIDevice, IDXGIFactory2, IDXGIKeyedMutex, IDXGIResource1, IDXGISwapChain, IDXGISwapChain1, IDXGISwapChain2, DXGI_CREATE_FACTORY_FLAGS, DXGI_ERROR_WAS_STILL_DRAWING, DXGI_FRAME_STATISTICS, DXGI_PRESENT, DXGI_PRESENT_DO_NOT_WAIT, DXGI_RGBA, DXGI_SCALING_NONE, DXGI_SWAP_CHAIN_DESC1, @@ -2095,6 +2095,30 @@ impl D3d11Window { self.latency_timeout_since = None; } + /// Whether this window's frame clock is ready for a frame right now: it is + /// unpaced, it already holds a credit, or its waitable is signaled. In the + /// last case the credit is taken here (a zero-timeout wait consumes one), so + /// the caller must go on to present this window, which spends it. + /// + /// Used during another window's beat: a secondary window whose pass is dirty + /// is normally held back for its own beat, but `MsgWaitForMultipleObjectsEx` + /// returns the lowest signaled handle, so while the primary keeps presenting + /// (a blinking caret is enough) the secondary's beat is never the one + /// returned and its frame would wait until the primary falls silent. + pub fn beat_ready(&self) -> bool { + if self.frame_latency_waitable.is_invalid() { + return true; + } + if try_with_win32_app(|app| app.has_beat_credit(self.window_id)).unwrap_or(false) { + return true; + } + let ready = unsafe { WaitForSingleObject(self.frame_latency_waitable, 0) } == WAIT_OBJECT_0; + if ready { + try_with_win32_app(|app| app.take_beat_credit(self.window_id)); + } + ready + } + pub fn start_resize(&mut self) { self.is_in_resize = true; // A live resize presents unpaced (Present(0) + DwmFlush), so its waitable @@ -2525,22 +2549,53 @@ impl D3d11Cx { // A DXGI factory snapshots its adapter enumeration when it is created, so one made // before a hybrid-GPU transition or a driver reinstall keeps handing back the // adapter that went away. Recovery always starts from a fresh factory. - let factory: IDXGIFactory2 = CreateDXGIFactory2(DXGI_CREATE_FACTORY_FLAGS(0))?; - let adapter = factory.EnumAdapters(0)?; + let mut factory: IDXGIFactory2 = CreateDXGIFactory2(DXGI_CREATE_FACTORY_FLAGS(0))?; + let force_software = std::env::var_os("MAKEPAD_D3D11_WARP") + .is_some_and(|value| value == "1") + || std::env::var_os("MAKEPAD_FORCE_SOFTWARE_GPU") + .is_some_and(|value| value == "1"); let mut device: Option = None; let mut context: Option = None; let mut query: Option = None; - D3D11CreateDevice( - &adapter, - D3D_DRIVER_TYPE_UNKNOWN, - HMODULE(std::ptr::null_mut()), - D3D11_CREATE_DEVICE_FLAG(0x800 | 0x20), // VIDEO_SUPPORT | BGRA_SUPPORT - Some(&[D3D_FEATURE_LEVEL_11_0]), - D3D11_SDK_VERSION, - Some(&mut device), - None, - Some(&mut context), - )?; + let hardware = if force_software { + None + } else { + Some(factory.EnumAdapters(0).and_then(|adapter| { + D3D11CreateDevice( + &adapter, + D3D_DRIVER_TYPE_UNKNOWN, + HMODULE(std::ptr::null_mut()), + D3D11_CREATE_DEVICE_FLAG(0x800 | 0x20), // VIDEO_SUPPORT | BGRA_SUPPORT + Some(&[D3D_FEATURE_LEVEL_11_0]), + D3D11_SDK_VERSION, + Some(&mut device), + None, + Some(&mut context), + ) + })) + }; + if hardware.as_ref().is_none_or(|result| result.is_err()) { + // A failed hardware attempt may have filled a partial COM + // handle. Release it before trying the system rasterizer. + device = None; + context = None; + D3D11CreateDevice( + None::<&windows::Win32::Graphics::Dxgi::IDXGIAdapter>, + D3D_DRIVER_TYPE_WARP, + HMODULE(std::ptr::null_mut()), + D3D11_CREATE_DEVICE_FLAG(0x20), // BGRA_SUPPORT; WARP needs no video interface + Some(&[D3D_FEATURE_LEVEL_11_0]), + D3D11_SDK_VERSION, + Some(&mut device), + None, + Some(&mut context), + )?; + // Swap chains must use the factory that owns this actual + // adapter, rather than the failed hardware adapter's factory. + let dxgi: IDXGIDevice = device.as_ref().unwrap().cast()?; + factory = dxgi.GetAdapter()?.GetParent()?; + crate::log!("D3D11: using Windows WARP software rendering"); + } let device = device.unwrap(); let context = context.unwrap(); diff --git a/platform/src/os/windows/win32_window.rs b/platform/src/os/windows/win32_window.rs index 01a356dce8..9433a686ee 100644 --- a/platform/src/os/windows/win32_window.rs +++ b/platform/src/os/windows/win32_window.rs @@ -795,7 +795,14 @@ impl Win32Window { let window = &mut (*(user_data as *mut Win32Window)); match msg { WM_ACTIVATE => { - if wparam.0 & 0xffff == WA_ACTIVE as usize { + // The low word is WA_INACTIVE (0), WA_ACTIVE (1) or WA_CLICKACTIVE (2). + // Activating a window by clicking it arrives as WA_CLICKACTIVE, so + // anything but WA_INACTIVE means the window gained focus. Matching + // WA_ACTIVE alone reported a click-activated window as having lost + // focus, which left a second window's buttons unresponsive. + const WA_INACTIVE: usize = 0; + debug_assert_eq!(WA_ACTIVE, 1); + if wparam.0 & 0xffff != WA_INACTIVE { window.do_callback(Win32Event::WindowGotFocus(window.window_id)); } else { if window.is_popup { diff --git a/platform/src/os/windows/windows.rs b/platform/src/os/windows/windows.rs index 2be479926b..40c04f7094 100644 --- a/platform/src/os/windows/windows.rs +++ b/platform/src/os/windows/windows.rs @@ -802,9 +802,20 @@ impl Cx { // may never come (an occluded window's presents are not // retired), and holding the pass back while another window // keeps flipping would leave the grab unanswered forever. + // ...and unless that window's own clock is already ready: the + // beat wait returns the lowest signaled handle, so a secondary + // window never wins it while the primary keeps presenting, and + // holding its pass back would leave it unpainted until the + // primary stops (seen as a second window whose button clicks + // took effect but never showed). `beat_ready` takes the credit; + // the present below spends it. if window_id != scope && paced.contains(&window_id) && !self.has_pending_window_screenshot(window_id) + && !d3d11_windows + .iter() + .find(|w| w.window_id == window_id) + .is_some_and(|w| w.beat_ready()) { self.repaint_pass(*draw_pass_id); continue; diff --git a/platform/src/script/res.rs b/platform/src/script/res.rs index a2da5c7c94..e5dbab134a 100644 --- a/platform/src/script/res.rs +++ b/platform/src/script/res.rs @@ -734,6 +734,26 @@ impl Cx { Err(e) => CxScriptResourceData::Error(e), }; return; + } else if let Some(dep_path) = res.dependency_path.as_deref() { + // `abs_path` holds the directory the resource had when it was + // registered -- the one the binary was built in. Only that + // machine has it, so a copy of a build found none of this + // crate's resources (nav-rail icons, the back arrow, article + // fonts, i18n tables) and drew a whole view without them, + // while the same binary on the build machine rendered them: + // there the path happens to exist. + // + // A desktop package lays every crate's assets under + // `/resources//...`, and `dependency_path` is + // the path inside that crate. `load_packaged_resource` above + // already searches there; this is the same search for a build + // that runs unpackaged, anchored at the executable instead. + if let Some(data) = crate::os::cx_native::read_file_cwd_or_exe_relative( + format!("resources/{dep_path}"), + ) { + res.data = CxScriptResourceData::Loaded(Rc::new(data)); + return; + } } } diff --git a/tools/cargo_makepad/src/android/java/dev/makepad/android/MakepadActivity.java b/tools/cargo_makepad/src/android/java/dev/makepad/android/MakepadActivity.java index 0a6c85d58c..dbf9811b3c 100644 --- a/tools/cargo_makepad/src/android/java/dev/makepad/android/MakepadActivity.java +++ b/tools/cargo_makepad/src/android/java/dev/makepad/android/MakepadActivity.java @@ -664,6 +664,13 @@ public void onGlobalLayout() { @SuppressWarnings("deprecation") @Override public boolean onKey(View v, int keyCode, KeyEvent event) { + // Back is not a surface key. Left unhandled it reaches the activity, + // which delivers it once through dispatchBackPressed: from onKeyUp -> + // onBackPressed below API 33, or from the OnBackInvokedCallback on + // API 33+ (which also carries the system edge gesture). Forwarding it + // here as well produced a second native BackPressed: one closed the + // current panel and the next left the app. + if (keyCode == KeyEvent.KEYCODE_BACK) return false; if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode != 0) { int metaState = event.getMetaState(); boolean isRepeat = event.getRepeatCount() > 0; @@ -1164,6 +1171,7 @@ public class MakepadActivity private FrameLayout mRootLayout; private FrameLayout mApplicationOverlay; private ApplicationExtension mApplicationExtension; + private android.window.OnBackInvokedCallback mBackInvokedCallback; /** Optional app Java client. Implementations enqueue blocking work on workers. */ public interface ApplicationExtension { @@ -1173,7 +1181,16 @@ public interface ApplicationExtension { void onIntent(Intent intent); void onDestroy(); default boolean onActivityResult(int requestCode, int resultCode, Intent data) { return false; } + /** Return true to consume Back before it reaches the native widget tree. */ default boolean onBackPressed() { return false; } + /** + * True when the extension registers its own Android 13+ + * OnBackInvokedCallback (calling {@link MakepadActivity#onBackPressed}). + * The activity then registers none and drops the compatibility Back + * key, so each Back is delivered once. Most extensions should leave + * this false and implement {@link #onBackPressed} instead. + */ + default boolean usesSystemBackCallback() { return false; } } private void createApplicationExtension() { @@ -1603,6 +1620,7 @@ public void onCreate(Bundle savedInstanceState) { } MakepadNative.activityOnCreate(this); createApplicationExtension(); + registerBackInvokedCallback(); registerPhysicalKeyboardListener(); mVideoPlaybackThread = new HandlerThread("VideoPlayerThread"); @@ -1707,6 +1725,7 @@ protected void onStop() { @Override protected void onDestroy() { + unregisterBackInvokedCallback(); if (mApplicationExtension != null) { mApplicationExtension.onDestroy(); mApplicationExtension = null; @@ -1789,6 +1808,12 @@ public void onLowMemory() { // key (BACK included) and forwards it to Rust, so onBackPressed never runs. @Override public boolean dispatchKeyEvent(KeyEvent event) { + // An extension that owns the system Back callback receives every Back + // through it; its compatibility key must not navigate a second time. + if (Build.VERSION.SDK_INT >= 33 && event.getKeyCode() == KeyEvent.KEYCODE_BACK + && extensionOwnsSystemBack()) { + return true; + } if (mQrScanning && event.getKeyCode() == KeyEvent.KEYCODE_BACK) { if (event.getAction() == KeyEvent.ACTION_UP) closeQrScanner("cancelled"); return true; @@ -1799,11 +1824,47 @@ public boolean dispatchKeyEvent(KeyEvent event) { @Override @SuppressWarnings("deprecation") public void onBackPressed() { + dispatchBackPressed(); + } + + /** The single delivery point for Back: key, button and system gesture. */ + private void dispatchBackPressed() { + if (mQrScanning) { + closeQrScanner("cancelled"); + return; + } if (mApplicationExtension != null && mApplicationExtension.onBackPressed()) return; - super.onBackPressed(); + // No super.onBackPressed(): Activity's default finishes (or, on API + // 31+, backgrounds) the activity before the widget tree has seen the + // event. Navigation belongs to the native BackPressed handlers. MakepadNative.onBackPressed(); } + private boolean extensionOwnsSystemBack() { + return mApplicationExtension != null && mApplicationExtension.usesSystemBackCallback(); + } + + // Android 13+ delivers Back, including the system edge gesture, to + // OnBackInvokedCallbacks when the manifest opts in + // (android:enableOnBackInvokedCallback, which cargo-makepad writes); + // onBackPressed is then no longer called. PRIORITY_DEFAULT leaves the + // IME's own Back (dismissing the keyboard) ahead of app navigation. + private void registerBackInvokedCallback() { + if (Build.VERSION.SDK_INT < 33 || mBackInvokedCallback != null || extensionOwnsSystemBack()) { + return; + } + mBackInvokedCallback = this::dispatchBackPressed; + getOnBackInvokedDispatcher().registerOnBackInvokedCallback( + android.window.OnBackInvokedDispatcher.PRIORITY_DEFAULT, mBackInvokedCallback); + } + + private void unregisterBackInvokedCallback() { + if (Build.VERSION.SDK_INT >= 33 && mBackInvokedCallback != null) { + getOnBackInvokedDispatcher().unregisterOnBackInvokedCallback(mBackInvokedCallback); + mBackInvokedCallback = null; + } + } + @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); diff --git a/tools/cargo_makepad/src/android/mod.rs b/tools/cargo_makepad/src/android/mod.rs index 3931c6c79d..1003138347 100644 --- a/tools/cargo_makepad/src/android/mod.rs +++ b/tools/cargo_makepad/src/android/mod.rs @@ -99,6 +99,7 @@ impl AndroidVariant { android:configChanges="orientation|screenSize|keyboardHidden" android:exported="true" android:launchMode="singleTask" + android:enableOnBackInvokedCallback="true" android:windowSoftInputMode="adjustNothing|stateUnchanged" android:theme="@style/MakepadLaunchTheme"> @@ -193,6 +194,7 @@ impl AndroidVariant { android:excludeFromRecents="false" android:exported="true" android:launchMode="singleTask" + android:enableOnBackInvokedCallback="true" android:screenOrientation="landscape" android:windowSoftInputMode="adjustNothing|stateUnchanged" android:theme="@style/MakepadLaunchTheme" @@ -209,6 +211,7 @@ impl AndroidVariant { android:excludeFromRecents="false" android:exported="true" android:launchMode="singleTask" + android:enableOnBackInvokedCallback="true" android:screenOrientation="landscape" android:windowSoftInputMode="adjustNothing|stateUnchanged" android:theme="@style/MakepadLaunchTheme" @@ -283,6 +286,7 @@ Self::Quest=>format!(r#" android:name="{class_name}" android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" android:launchMode="singleTask" + android:enableOnBackInvokedCallback="true" android:screenOrientation="landscape" android:excludeFromRecents="false" android:configChanges="screenSize|screenLayout|orientation|keyboardHidden|keyboard|navigation|uiMode" @@ -301,6 +305,7 @@ Self::Quest=>format!(r#" android:excludeFromRecents="false" android:exported="true" android:launchMode="singleTask" + android:enableOnBackInvokedCallback="true" android:screenOrientation="landscape" android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" > @@ -927,6 +932,22 @@ mod tests { assert!(xml.contains("android:launchMode=\"singleTask\"")); } + #[test] + fn every_activity_opts_into_back_invoked_callbacks() { + // MakepadActivity registers one OnBackInvokedCallback on API 33+; + // without the opt-in Android ignores it and finishes the activity + // on a system Back gesture instead. + for variant in [AndroidVariant::Default, AndroidVariant::Quest] { + let xml = variant.manifest_xml(&test_args("App", "dev.makepad.app")); + assert_eq!( + xml.matches("android:launchMode=\"singleTask\"").count(), + xml.matches("android:enableOnBackInvokedCallback=\"true\"").count(), + "every activity needs the Back opt-in: {xml}" + ); + assert!(xml.contains("android:enableOnBackInvokedCallback=\"true\"")); + } + } + #[test] fn quest_manifest_uses_splash_and_app_themes() { let xml = AndroidVariant::Quest.manifest_xml(&test_args("App", "dev.makepad.app")); diff --git a/tools/open_harmony/deveco/entry/src/main/cpp/types/libentry/Index.d.ts b/tools/open_harmony/deveco/entry/src/main/cpp/types/libentry/Index.d.ts index acc79305a2..b0cd527d31 100644 --- a/tools/open_harmony/deveco/entry/src/main/cpp/types/libentry/Index.d.ts +++ b/tools/open_harmony/deveco/entry/src/main/cpp/types/libentry/Index.d.ts @@ -1,5 +1,11 @@ export const onCreate: (ark_ts: object) => void; -export const handleInsertTextEvent: (text:String) => void; -export const handleDeleteLeftEvent: (length: number) => void +export const mountContent: (content: object) => void +export const webViewList: () => Array; +export const webViewTakeCommands: () => Array; +export const webViewAttached: (tag: string) => void; +export const webViewReady: (tag: string) => void; +export const webViewMayNavigate: (tag: string, url: string) => boolean; +export const webViewNavigation: (tag: string, url: string, title: string, loading: boolean) => void; +export const webViewPageError: (tag: string, code: number, description: string, url: string) => void; export const handleKeyboardStatus: (isOpen:boolean, keyboardHeight:number) => void; export const handleLocation: (lat: number, lon: number, accuracy: number) => void; diff --git a/tools/open_harmony/deveco/entry/src/main/ets/makepad/makepad.ets b/tools/open_harmony/deveco/entry/src/main/ets/makepad/makepad.ets index 5de5a719ba..aeea02b78e 100644 --- a/tools/open_harmony/deveco/entry/src/main/ets/makepad/makepad.ets +++ b/tools/open_harmony/deveco/entry/src/main/ets/makepad/makepad.ets @@ -1,7 +1,5 @@ -import display from '@ohos.display'; -import deviceInfo from '@ohos.deviceInfo'; -import inputMethod from '@ohos.inputMethod'; import { window } from '@kit.ArkUI'; +import { webview } from '@kit.ArkWeb'; import common from '@ohos.app.ability.common'; import resourceManager from '@ohos.resourceManager'; import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl'; @@ -13,37 +11,21 @@ declare type Any = ESObject; export interface MakepadXComponentInterface{ onCreate(ark_ts: object):void; - handleInsertTextEvent(text:String):void; - handleDeleteLeftEvent(length: number):void; + mountContent(content: object):void; handleKeyboardStatus(isOpen:boolean, keyboardHeight:number):void; handlePermissionResult(permission: string, granted: boolean, canRetry: boolean): void; handleAvoidArea(top: number, right: number, bottom: number, left: number): void; takeCaptureToSave(): string; + webViewList(): Array; + webViewTakeCommands(): Array; + webViewAttached(tag: string): void; + webViewReady(tag: string): void; + webViewMayNavigate(tag: string, url: string): boolean; + webViewNavigation(tag: string, url: string, title: string, loading: boolean): void; + webViewPageError(tag: string, code: number, description: string, url: string): void; handleCaptureSaved(path: string, ok: boolean, uri: string): void; } - -function getDensity(): number { - try { - let displayClass = display.getDefaultDisplaySync(); - console.info('Test densityDPI:' + JSON.stringify(displayClass.densityDPI)); - return displayClass.densityDPI / 160; - } catch (exception) { - console.error('Failed to obtain the default display object. Code: ' + JSON.stringify(exception)); - return 3; - } -} - -function getDeviceType(): string { - let device_type: string = deviceInfo.deviceType; - if (device_type == "") { - console.error("deviceInfo.deviceType is empty string!") - } else { - console.info("Device type is " + device_type) - } - return device_type; -} - export class ArkGlue{ private static instance : ArkGlue; static initInstance(context: common.Context) : ArkGlue { @@ -59,10 +41,9 @@ export class ArkGlue{ context: common.Context; - deviceType = getDeviceType(); - osFullName = deviceInfo.osFullName; - displayDensity = getDensity(); resMgr : resourceManager.ResourceManager; + // The module's directories (haps//...): the NDK only reports the + // application's, and the app's data lives here. filesDir : string; cacheDir : string; tempDir : string; @@ -74,9 +55,6 @@ export class ArkGlue{ // XComponent has loaded (the window reports them before that). private avoidArea: number[] = [0, 0, 0, 0]; - private inputMethodController : inputMethod.InputMethodController; - private textConfig: inputMethod.TextConfig; - private imcFlag = false; constructor(context: common.Context) { this.context = context; @@ -84,17 +62,6 @@ export class ArkGlue{ this.filesDir = this.context.filesDir; this.cacheDir = this.context.cacheDir; this.tempDir = this.context.tempDir; - this.inputMethodController = inputMethod.getController(); - this.textConfig = { - inputAttribute: { - textInputType: 0, - enterKeyType: 1 - } - }; - } - - showKeyBoard(): void { - this.showTextInput(); } // EntryAbility reports foreground/background so a location session can @@ -103,10 +70,6 @@ export class ArkGlue{ setLocationForeground(_foreground: boolean): void { } - hideKeyBoard(): void { - this.hideTextInput(); - } - // A photo or video the native app wrote into its sandbox: offer it to the // system gallery (the user confirms in the system dialog) and copy it over. saveLatestCapture(): void { @@ -173,6 +136,7 @@ export class ArkGlue{ pushAvoidArea(): void { const a = this.avoidArea; this.xComponentContext?.handleAvoidArea(a[0], a[1], a[2], a[3]); + this.listenKeyboardHeightChange(); } // Runtime (user_grant) permissions: Rust calls one of these without @@ -205,86 +169,86 @@ export class ArkGlue{ }); } - private async showTextInput(): Promise { - this.listenKeyboardHeightChange(); - await this.attach(true); - if (this.imcFlag != true) { - this.listenKeyBoardEvent(); + // ---- System browsers (platform oh_web.rs) ---- + // The NDK has no web node, so the page declares one `Web` per browser from + // the list Rust keeps; ArkWeb's NDK then serves the page bridge and script + // evaluation natively. What it cannot do (load a URL or HTML, history) + // Rust queues and this runs. + private webControllers: Map = new Map(); + private webTags: Array = []; + + webController(tag: string): webview.WebviewController { + let controller = this.webControllers.get(tag); + if (controller === undefined) { + // Named with the tag: Rust addresses this webview by it through the NDK. + controller = new webview.WebviewController(tag); + this.webControllers.set(tag, controller); } - this.inputMethodController.showTextInput().then(() => { - console.debug("Succeeded in showing softKeyboard"); - }).catch((err: Any) => { - console.error("Failed to show softKeyboard:" + JSON.stringify(err)); - }); - } - - private async hideTextInput(): Promise { - this.inputMethodController.hideTextInput().then(() => { - console.debug("Succeeded in hide softKeyboard"); - }).catch((err: Any) => { - console.error("Failed to hide softKeyboard:" + JSON.stringify(err)); - }) - this.cancelKeyboardHeightChange(); + return controller; } - private async attach(showKeyboard: boolean): Promise { - try { - await this.inputMethodController.attach(showKeyboard, this.textConfig); - } catch (err) { - console.error("Failed to attach:" + JSON.stringify(err)); + // Rust calls this when the browsers changed or loads are due. + syncWebViews(): void { + const list: Array = this.xComponentContext?.webViewList() ?? []; + const tags: Array = []; + const geometry: Record = {}; + for (const entry of list) { + const i = entry.indexOf('|'); + tags.push(entry.substring(0, i)); + geometry[entry.substring(0, i)] = entry.substring(i + 1); + } + if (tags.join(',') !== this.webTags.join(',')) { + for (const tag of this.webTags) { + if (tags.indexOf(tag) < 0) { + this.webControllers.delete(tag); + } + } + this.webTags = tags; + AppStorage.setOrCreate('makepadWebTags', tags); } + AppStorage.setOrCreate('makepadWebGeometry', geometry); + this.flushWebCommands(); } - private listenKeyBoardEvent(): void { - try { - this.inputMethodController.on('insertText', (text) => { - this.xComponentContext?.handleInsertTextEvent(text); - console.debug("insertText: " + text); - }); - } catch (err) { - console.error("Failed to subscribe insertText:" + JSON.stringify(err)); - this.cancelListenKeyBoardEvent(); - return; - } + webAttached(tag: string): void { + this.xComponentContext?.webViewAttached(tag); + } - try { - this.inputMethodController.on('deleteLeft', (length) => { - this.xComponentContext?.handleDeleteLeftEvent(length); - console.debug("deleteLeft, length:" + JSON.stringify(length)); - }) - } catch (err) { - console.error("Failed to subscribe deleteLeft:" + JSON.stringify(err)); - this.cancelListenKeyBoardEvent(); - return; - } + // The first page (about:blank) finished: loading earlier would race it and + // be aborted. + webReady(tag: string): void { + this.xComponentContext?.webViewReady(tag); + this.flushWebCommands(); + } - try { - this.inputMethodController.on('sendKeyboardStatus', (state) => { - if (state == inputMethod.KeyboardStatus.HIDE) { - this.xComponentContext?.handleKeyboardStatus(false, 0); - this.inputMethodController.detach(); - this.cancelListenKeyBoardEvent(); + private flushWebCommands(): void { + const commands: Array = this.xComponentContext?.webViewTakeCommands() ?? []; + for (const command of commands) { + const a = command.indexOf('|'); + const b = command.indexOf('|', a + 1); + const tag = command.substring(0, a); + const kind = command.substring(a + 1, b); + const payload = command.substring(b + 1); + try { + const controller = this.webController(tag); + if (kind === 'url') { + controller.loadUrl(payload); + } else if (kind === 'html') { + const c = payload.indexOf('|'); + controller.loadData(payload.substring(c + 1), 'text/html', 'UTF-8', payload.substring(0, c), ' '); + } else if (kind === 'history') { + controller.backOrForward(Number(payload)); } - console.debug("sendKeyboardStatus, state:" + JSON.stringify(state)); - }) - } catch (err) { - console.error("Failed to subscribe sendKeyboardStatus:" + JSON.stringify(err)); - this.cancelListenKeyBoardEvent(); - return; + } catch (err) { + console.error('makepad web ' + kind + ' failed: ' + JSON.stringify(err)); + } } - - console.debug("listenKeyBoardEvent success"); - this.imcFlag = true; - } - - private cancelListenKeyBoardEvent(): void { - this.inputMethodController.off('insertText'); - this.inputMethodController.off('deleteLeft'); - this.inputMethodController.off('sendKeyboardStatus'); - this.imcFlag = false; } + // The input method itself is native (platform oh_ime.rs, the NDK's + // libohinputmethod); only the window's keyboard height has no NDK callback. private keyboardHeightListening = false; + private listenKeyboardHeightChange() { // Once per attach cycle: every ShowTextIME used to add another listener. if (this.keyboardHeightListening) { return; } @@ -304,21 +268,4 @@ export class ArkGlue{ }) } - private cancelKeyboardHeightChange() { - this.keyboardHeightListening = false; - window.getLastWindow(getContext(this)).then(currentWindow => { - try { - currentWindow.off('keyboardHeightChange'); - } catch (exception) { - console.error(`Failed to enable the listener for keyboard height changes. Cause code: ${exception.code}, message: ${exception.message}`); - } - }) - } - -} - -export interface XComponentAttrs { - id: string; - type: number; - libraryname: string; } diff --git a/tools/open_harmony/deveco/entry/src/main/ets/pages/Index.ets b/tools/open_harmony/deveco/entry/src/main/ets/pages/Index.ets index 695eac7122..7f3d1c2cf8 100644 --- a/tools/open_harmony/deveco/entry/src/main/ets/pages/Index.ets +++ b/tools/open_harmony/deveco/entry/src/main/ets/pages/Index.ets @@ -1,29 +1,77 @@ -import {ArkGlue, MakepadXComponentInterface, XComponentAttrs} from '../makepad/makepad'; +import { NodeContent } from '@kit.ArkUI'; +import makepad from 'libmakepad.so'; +import { ArkGlue, MakepadXComponentInterface } from '../makepad/makepad'; +// ArkTS's part in makepad's UI: one ContentSlot, which native code fills +// (node_shim.cpp: the XComponent, its surface, input and scrolling), and one +// `Web` per system browser above it, because the NDK has no web node. Rust +// keeps the browsers (oh_web.rs); their page bridge and script evaluation go +// through ArkWeb's NDK (web_shim.cpp), not through here. @Entry @Component struct Index { - xComponentContext: MakepadXComponentInterface | undefined = undefined; - xComponentAttrs: XComponentAttrs = { - id: 'Entry', - type: XComponentType.SURFACE, - libraryname: 'entry', + private content: NodeContent = new NodeContent(); + @StorageLink('makepadWebTags') webTags: Array = []; + @StorageLink('makepadWebGeometry') webGeometry: Record = {}; + + aboutToAppear(): void { + const native = makepad as MakepadXComponentInterface; + native.mountContent(this.content); + ArkGlue.getInstance().xComponentContext = native; + ArkGlue.getInstance().pushAvoidArea(); + } + + // "x|y|w|h|visible" in vp. + private geometry(tag: string, index: number): number { + const g = this.webGeometry[tag]; + return g === undefined ? 0 : Number(g.split('|')[index]); } build() { - Row() { - Column() { - XComponent(this.xComponentAttrs) - .focusable(true) - .onLoad((xComponentContext) => { - this.xComponentContext = xComponentContext as MakepadXComponentInterface; - ArkGlue.getInstance().xComponentContext = this.xComponentContext; - ArkGlue.getInstance().pushAvoidArea(); - //this.xComponentContext.initMakepad(ArkGlue.getInstance()); + Stack({ alignContent: Alignment.TopStart }) { + ContentSlot(this.content) + ForEach(this.webTags, (tag: string) => { + Web({ src: 'about:blank', controller: ArkGlue.getInstance().webController(tag) }) + .javaScriptAccess(true) + .domStorageAccess(true) + .mediaPlayGestureAccess(false) + .onControllerAttached(() => { + ArkGlue.getInstance().webAttached(tag); + }) + // Returning true cancels: a browser opened with `spawn` stays on the + // documents the app loads. + .onLoadIntercept((event) => { + const native = makepad as MakepadXComponentInterface; + return !native.webViewMayNavigate(tag, event.data.getRequestUrl()); + }) + // about:blank is the placeholder each Web starts on, not a page the + // app asked for; its end means the browser can take loads. + .onPageBegin((event) => { + if (event.url !== 'about:blank') { + (makepad as MakepadXComponentInterface).webViewNavigation(tag, event.url, '', true); + } + }) + .onPageEnd((event) => { + if (event.url === 'about:blank') { + ArkGlue.getInstance().webReady(tag); + return; + } + const title = ArkGlue.getInstance().webController(tag).getTitle(); + (makepad as MakepadXComponentInterface).webViewNavigation(tag, event.url, title, false); + }) + .onErrorReceive((event) => { + if (event.request.isMainFrame()) { + (makepad as MakepadXComponentInterface).webViewPageError(tag, event.error.getErrorCode(), + event.error.getErrorInfo(), event.request.getRequestUrl()); + } }) - } - .width('100%') + .position({ x: this.geometry(tag, 0), y: this.geometry(tag, 1) }) + .width(Math.max(1, this.geometry(tag, 2))) + .height(Math.max(1, this.geometry(tag, 3))) + .visibility(this.geometry(tag, 4) === 1 ? Visibility.Visible : Visibility.Hidden) + }, (tag: string) => tag) } + .width('100%') .height('100%') } -} \ No newline at end of file +} diff --git a/widgets/src/widget_async.rs b/widgets/src/widget_async.rs index f0ddb5efa4..664f94c5bb 100644 --- a/widgets/src/widget_async.rs +++ b/widgets/src/widget_async.rs @@ -417,6 +417,14 @@ fn with_isolate_installed(cx: &mut Cx, vm_id: SplashVmId, f: impl FnOnce(&mut // app its VM and the table its isolate. let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(cx))); + // Drain promise/task resumptions `f` queued on the isolate while it is + // still the installed VM — see pump_installed_isolate_tasks. Without + // this, an `await` woken by a timer or network callback never resumes + // while the app is idle (OctoSense-App-Hub #213). + if out.is_ok() && cx.script_vm.is_some() { + pump_isolate_tasks_under_budget(cx, isolated.time_budget); + } + isolated.vm = cx.script_vm.take(); cx.script_vm = outer_vm; isolated.std = std::mem::replace(&mut cx.script_data.std, outer_std); @@ -458,6 +466,23 @@ pub fn contain_isolate_panic(what: &str, f: impl FnOnce()) -> bool { /// ([`CxSplashVmExt::set_splash_vm_time_budget`]). pub const DEFAULT_SPLASH_TIME_BUDGET: std::time::Duration = std::time::Duration::from_millis(64); +/// The budget an isolate starts with: [`DEFAULT_SPLASH_TIME_BUDGET`], unless +/// `MAKEPAD_SPLASH_BUDGET_MS` says otherwise. The budget is wall-clock time, +/// so it also counts time the entry spends waiting for the CPU or a lock. A +/// test binary running a thousand tests in one process is the case for the +/// override: there the wall clock says little about the script. A host's own +/// budget for an isolate still takes its place. +fn splash_entry_budget() -> std::time::Duration { + static BUDGET: std::sync::OnceLock = std::sync::OnceLock::new(); + *BUDGET.get_or_init(|| { + std::env::var("MAKEPAD_SPLASH_BUDGET_MS") + .ok() + .and_then(|value| value.trim().parse::().ok()) + .filter(|&ms| ms > 0) + .map_or(DEFAULT_SPLASH_TIME_BUDGET, std::time::Duration::from_millis) + }) +} + /// A Splash isolate runs untrusted-ish user script on the UI thread; cap how long any /// single entry into it may run. The cap is the isolate's own `budget`. fn with_splash_budget( @@ -481,7 +506,7 @@ fn splash_vm_time_budget(cx: &mut Cx, vm_id: SplashVmId) -> std::time::Duration .isolated_vms .vms .get(&vm_id) - .map_or(DEFAULT_SPLASH_TIME_BUDGET, |isolated| isolated.time_budget) + .map_or_else(splash_entry_budget, |isolated| isolated.time_budget) } pub trait CxSplashVmExt { @@ -623,7 +648,7 @@ impl CxSplashVmExt for Cx { id, IsolatedSplashVm { network_enabled, - time_budget: DEFAULT_SPLASH_TIME_BUDGET, + time_budget: splash_entry_budget(), std, vm: Some(bx), }, @@ -1734,6 +1759,9 @@ impl<'a, 'b> IsolateCx for Cx2d<'a, 'b> { pub fn with_isolate(cx: &mut C, vm_id: SplashVmId, f: impl FnOnce(&mut C) -> R) -> R { let entry = enter_isolate(cx.isolate_cx(), vm_id); let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(cx))); + if out.is_ok() { + pump_installed_isolate_tasks(cx.isolate_cx(), &entry); + } leave_isolate(cx.isolate_cx(), entry); match out { Ok(out) => out, @@ -1767,6 +1795,43 @@ pub fn enter_isolate(cx: &mut Cx, vm_id: SplashVmId) -> IsolateEntry { IsolateEntry { vm_id, previous_vm_id, network_enabled: isolated.network_enabled, time_budget: isolated.time_budget, outer_std: Some(outer_std), outer_vm } } +/// Drain the installed isolate's script task queue: promise resolutions and +/// task send/recv resumptions queued while `f` ran (a timer callback firing +/// `resolve`, a network response landing, a widget call answering) start the +/// paused threads they woke. The main VM gets this drain after every event +/// dispatch (`call_event_handler`); an isolate has no such beat of its own, +/// so without it an `await` resumed by anything other than a user event +/// would only run the next time something happened to enter the isolate — +/// for an idle app, possibly never (OctoSense-App-Hub #213). +/// +/// Only called on the success path: after a guest panic the thread list may +/// be mid-teardown, and the host tears the isolate down anyway. A nested or +/// main-VM entry skips it (the outermost entry drains). +fn pump_installed_isolate_tasks(cx: &mut Cx, entry: &IsolateEntry) { + if entry.vm_id == MAIN_SPLASH_VM_ID || cx.script_vm.is_none() { + return; + } + pump_isolate_tasks_under_budget(cx, entry.time_budget); +} + +/// The drain itself, for the isolate that is the installed VM. A task it +/// resumes is guest script like any other entry into the isolate, so the drain +/// runs under the isolate's own time budget: without one, a resumed task that +/// never yields would hold the UI thread for good. +fn pump_isolate_tasks_under_budget(cx: &mut Cx, budget: std::time::Duration) { + let old_budget = cx.with_vm(|vm| { + vm.bx + .run_budget + .replace(ScriptRunBudget::from_durations(budget, budget, 512)) + }); + let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + crate::makepad_draw::makepad_platform::makepad_script_std::pump(cx); + })); + if cx.script_vm.is_some() { + cx.with_vm(|vm| vm.bx.run_budget = old_budget); + } +} + /// Put the outer VM back. See [`enter_isolate`]. pub fn leave_isolate(cx: &mut Cx, entry: IsolateEntry) { if entry.vm_id == MAIN_SPLASH_VM_ID { @@ -2079,6 +2144,82 @@ mod isolate_entry_tests { assert_eq!(splash_vm_time_budget(&mut cx, vm_id), raised); cx.free_splash_vm(vm_id); } + + /// Regression pin for OctoSense-App-Hub #213: a promise resolved while + /// host code is inside the isolate (a timer callback, a network event) + /// must resume its awaiting task on the way OUT — an isolate has no + /// event-dispatch beat of its own to drain the queue later. Fails when + /// the exit pump in `with_isolate_installed` is removed. + #[test] + fn a_resolution_queued_during_an_isolate_entry_resumes_on_exit() { + let mut cx = Cx::new(Box::new(|_, _| {})); + cx.with_vm(crate::script_mod); + let vm_id = cx.alloc_splash_vm(); + + // Entry 1: a task awaits a promise. The exit pump starts the task; + // it parks on the empty promise. + cx.with_script_vm_id(vm_id, |vm| { + let v = vm.eval(script! { + use mod.std + mod.state = {} + mod.state.p = std.promise() + std.task(|_t| { + mod.state.resolved = mod.state.p.await() + }) + }); + assert!(!v.is_err(), "entry eval failed: {v:?}"); + }); + + // Entry 2: resolve the promise (what a timer or network callback + // does). The exit pump must resume the parked task. + cx.with_script_vm_id(vm_id, |vm| { + vm.eval(script! { mod.state.p.resolve(42.5) }); + }); + + let resolved = cx.with_script_vm_id(vm_id, |vm| { + vm.eval(script! { mod.state.resolved }) + }); + assert_eq!(resolved.as_f64(), Some(42.5), "the awaiting task resumed with the resolved value"); + + cx.free_splash_vm(vm_id); + } + + /// A task the exit drain resumes runs under the isolate's time budget + /// like any entry: one that never yields is stopped, and the isolate + /// still answers afterwards. + #[test] + fn a_task_resumed_on_exit_is_stopped_at_the_isolates_time_budget() { + let mut cx = Cx::new(Box::new(|_, _| {})); + cx.with_vm(crate::script_mod); + let vm_id = cx.alloc_splash_vm(); + + cx.with_script_vm_id(vm_id, |vm| { + let v = vm.eval(script! { + use mod.std + mod.state = {} + mod.state.p = std.promise() + std.task(|_t| { + mod.state.p.await() + loop {} + }) + }); + assert!(!v.is_err(), "entry eval failed: {v:?}"); + }); + + let start = std::time::Instant::now(); + cx.with_script_vm_id(vm_id, |vm| { + vm.eval(script! { mod.state.p.resolve(1.0) }); + }); + let stopped_after = start.elapsed(); + assert!(stopped_after >= DEFAULT_SPLASH_TIME_BUDGET, "{stopped_after:?}"); + assert!(stopped_after < std::time::Duration::from_secs(5), "{stopped_after:?}"); + assert!(cx.with_vm(|vm| vm.bx.run_budget.is_none()), "the app VM is not budgeted"); + + let two = cx.with_script_vm_id(vm_id, |vm| vm.eval(script! { 1 + 1 })); + assert_eq!(two.as_f64(), Some(2.0), "the isolate still answers"); + + cx.free_splash_vm(vm_id); + } } #[cfg(test)] diff --git a/widgets/tests/vm_query_corpus.rs b/widgets/tests/vm_query_corpus.rs new file mode 100644 index 0000000000..43790a446f --- /dev/null +++ b/widgets/tests/vm_query_corpus.rs @@ -0,0 +1,164 @@ +//! The widgets-tagged half of the VM query API fixture corpus — ADR 0001 §4, +//! App Flow #9. +//! +//! A real Splash isolate installs more than `makepad-script` itself does: the +//! widget registry (`mod.prelude.widgets`, `mod.widgets`), the host doorway +//! (`mod.host`), the jailed per-app storage (`mod.fs`), the `sys` module, the +//! agent module, and the Cx-free runtime's `net`. Those are the names a +//! completion provider asks about most, so the corpus covers them here, on the +//! isolate the host really builds — the same one `Splash` widgets run in. +//! +//! This runner reads the same corpus file as +//! `platform/script/tests/query_api_corpus.rs` (which checks the `script` +//! entries with `cargo test -p makepad-script`); this one runs with +//! `cargo test -p makepad-widgets`. Regenerate its entries with +//! `QUERY_CORPUS_WRITE=1`. + +use makepad_widgets::makepad_script::{query, ScriptVm}; +use makepad_widgets::{Cx, CxSplashVmExt}; + +const CORPUS: &str = include_str!("../../platform/script/tests/query_corpus.txt"); +const RUNNER: &str = "widgets"; +const CORPUS_PATH: &str = concat!( + env!("CARGO_MANIFEST_DIR"), + "/../platform/script/tests/query_corpus.txt" +); + +/// The isolate modules the corpus names. ADR 0001 §4 lists `mod.prelude.widgets`, +/// `mod.host`, `mod.fs`, `mod.sys` and `mod.net` for a real isolate (`mod.std` +/// is the script crate's entry, checked there); the widget registry and the +/// agent module complete the set a completion provider asks about. +const ISOLATE_MODULES: &[&str] = &[ + "mod.agent", + "mod.fs", + "mod.host", + "mod.net", + "mod.prelude.widgets", + "mod.sys", + "mod.widgets", +]; + +/// Run `f` inside a real Splash isolate: the VM the host allocates for a +/// widget, with the widget registry, the host doorway, the jailed storage and +/// the runtime modules installed. `sys` and `agent` are registered per isolate +/// by the host (`splash::register_agent_module`), the way the hosts that embed +/// a Splash do — see the isolate test in `widgets/src/splash.rs`. +fn with_isolate(f: impl FnOnce(&mut ScriptVm) -> R) -> R { + let mut cx = Cx::new(Box::new(|_, _| {})); + let vm_id = cx.alloc_splash_vm_with_network(true); + cx.with_script_vm_id(vm_id, |vm| { + makepad_widgets::splash::register_agent_module(vm); + f(vm) + }) +} + +struct Entry { + name: String, + runner: String, + keys: Vec, + total: usize, +} + +fn parse_corpus(text: &str) -> Vec { + text.lines() + .map(|line| line.trim()) + .filter(|line| !line.is_empty() && !line.starts_with('#')) + .map(|line| { + let mut fields = line.split('\t'); + let name = fields.next().unwrap_or_default().to_string(); + let runner = fields.next().unwrap_or_default().to_string(); + let keys = fields + .next() + .unwrap_or_default() + .split(',') + .filter(|key| !key.is_empty()) + .map(|key| key.to_string()) + .collect(); + let total = fields + .next() + .and_then(|total| total.parse().ok()) + .unwrap_or(0); + Entry { name, runner, keys, total } + }) + .collect() +} + +#[test] +fn the_corpus_matches_the_modules_a_real_isolate_installs() { + with_isolate(|vm| { + if std::env::var_os("QUERY_CORPUS_WRITE").is_some() { + regenerate(vm, CORPUS); + return; + } + + let entries: Vec = parse_corpus(CORPUS) + .into_iter() + .filter(|entry| entry.runner == RUNNER) + .collect(); + + for name in ISOLATE_MODULES { + assert!( + entries.iter().any(|entry| entry.name == *name), + "the corpus no longer names {name}: the ADR's module list is the \ + contract, and a runner that checks fewer names is a corpus that \ + shrank without anyone saying so" + ); + } + + for entry in &entries { + let module = query::module_object(&vm.bx.heap, &entry.name) + .unwrap_or_else(|| panic!("the corpus names {}, which this isolate does not install", entry.name)); + let keys = query::object_keys(&vm.bx.heap, module); + let total = vm.bx.heap.iter_len(module); + assert_eq!( + keys, entry.keys, + "corpus drift for {}: object_keys answers with a different set \ + (if the module's members changed on purpose, regenerate with \ + QUERY_CORPUS_WRITE=1 and name the diff in the pull request)", + entry.name + ); + // The total is recorded because a name is not always recoverable — + // a key registered in Rust with `id!` and never spelled by source is + // an id the VM cannot name, and `object_keys` does not invent one. + // Recording the count keeps such an entry a visible diff. + assert_eq!( + total, entry.total, + "corpus drift for {}: the object holds {} entries, not the {} the \ + corpus records", + entry.name, total, entry.total + ); + } + }); +} + +/// Rewrite this runner's entries from the isolate, leaving every other line — +/// the header, the comments and the script runner's entries — exactly as it +/// stands. Run the test again afterwards: a regenerated corpus is a diff a +/// person reads and names in the pull request, not a green checkmark. +fn regenerate(vm: &ScriptVm, corpus: &str) { + let mut out = String::new(); + for line in corpus.lines() { + let entry = parse_corpus(line).into_iter().next(); + let rewritten = match entry { + Some(entry) if entry.runner == RUNNER => { + match query::module_object(&vm.bx.heap, &entry.name) { + Some(module) => format!( + "{}\t{}\t{}\t{}", + entry.name, + entry.runner, + query::object_keys(&vm.bx.heap, module).join(","), + vm.bx.heap.iter_len(module) + ), + // Not installed any more: leave the line for the check to + // report, rather than silently dropping it. + None => line.to_string(), + } + } + _ => line.to_string(), + }; + out.push_str(&rewritten); + out.push('\n'); + } + std::fs::write(CORPUS_PATH, out).expect("write the regenerated corpus"); + println!("regenerated {CORPUS_PATH}"); +}