diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ce5bdecf..56e29bca 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -40,6 +40,8 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + persist-credentials: false - name: Set up Node.js ${{ matrix.node-version }} uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 @@ -80,6 +82,8 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + persist-credentials: false - name: Set up Node.js uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 @@ -100,10 +104,28 @@ jobs: - name: Rustfmt check run: cargo fmt --manifest-path compiler/Cargo.toml -- --check - - name: Cargo check (default & JNI features) + - name: Cargo check (default, JNI, and Boa-free kernels features) run: | cargo check --manifest-path compiler/Cargo.toml --all-targets cargo check --manifest-path compiler/Cargo.toml --features jni + cargo check --manifest-path compiler/Cargo.toml --no-default-features --features kernels,jni --lib + + - name: Build Boa-free kernels library and assert zero Boa symbols + run: | + if cargo tree --manifest-path compiler/Cargo.toml -e normal --no-default-features --features kernels,jni | grep -q "boa_engine"; then + echo "::error::Boa-free kernels dependency graph includes boa_engine!" + exit 1 + fi + cargo build --manifest-path compiler/Cargo.toml --no-default-features --features kernels,jni --lib + cargo test --manifest-path compiler/Cargo.toml --no-default-features --features kernels,jni --lib -- --test-threads=1 + if nm target/debug/libthree_live_compiler.so | grep -i "boa_engine"; then + echo "::error::Boa-free kernels library leaked boa_engine symbols!" + exit 1 + fi + if ! readelf -d target/debug/libthree_live_compiler.so | grep -q "libthree_live_kernels.so"; then + echo "::error::Expected SONAME libthree_live_kernels.so on Boa-free kernels cdylib" + exit 1 + fi # The debug profile is the primary gate: it is the only one where `overflow-checks` # and `debug_assert!` are live, which matters for a crate doing offset and size @@ -137,6 +159,8 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + persist-credentials: false - name: Set up Java 17 uses: actions/setup-java@cf277c60eb25467037889841efdb72551f06f6c3 # v4 @@ -154,10 +178,10 @@ jobs: gradle-version: '8.7' - name: Run Android unit tests - run: gradle :mobile:android:testReleaseUnitTest :mobile:android-filament:testReleaseUnitTest :mobile:android-firebase-ai:testReleaseUnitTest :mobile:android-detect-mlkit:testReleaseUnitTest :mobile:android-hands-androidxr:testReleaseUnitTest :mobile:android-hands-mediapipe:testReleaseUnitTest --stacktrace + run: gradle :mobile:android-runtime:testReleaseUnitTest :mobile:android-kernels:testReleaseUnitTest :mobile:android:testReleaseUnitTest :mobile:android-filament:testReleaseUnitTest :mobile:android-firebase-ai:testReleaseUnitTest :mobile:android-detect-mlkit:testReleaseUnitTest :mobile:android-hands-androidxr:testReleaseUnitTest :mobile:android-hands-mediapipe:testReleaseUnitTest --stacktrace - - name: Assemble Android release AARs (core + optional Filament / Firebase AI / ML Kit / Hands modules) - run: gradle :mobile:android:assembleRelease :mobile:android-filament:assembleRelease :mobile:android-firebase-ai:assembleRelease :mobile:android-detect-mlkit:assembleRelease :mobile:android-hands-androidxr:assembleRelease :mobile:android-hands-mediapipe:assembleRelease --stacktrace + - name: Assemble Android release AARs (runtime, kernels, compiler + optional adapter modules) + run: gradle :mobile:android-runtime:assembleRelease :mobile:android-kernels:assembleRelease :mobile:android:assembleRelease :mobile:android-filament:assembleRelease :mobile:android-firebase-ai:assembleRelease :mobile:android-detect-mlkit:assembleRelease :mobile:android-hands-androidxr:assembleRelease :mobile:android-hands-mediapipe:assembleRelease --stacktrace # The example app is not shipped, but it is the first thing a new user runs, and it is the # only consumer of the library's public API. Compiling it here is what catches breakage in @@ -171,6 +195,8 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + persist-credentials: false # The SwiftPM target needs its own copy of the C header. A divergence between the two # copies is an ABI bug that no compiler would catch, so assert they are identical. @@ -204,11 +230,68 @@ jobs: - name: Run Swift package tests run: swift test + - name: Verify standalone ThreeLiveRuntime + ThreeLiveKernels consumer (zero Boa linked) + run: | + set -euo pipefail + REPO_ROOT="$PWD" + PKG_ID="$(basename "${REPO_ROOT}" | tr '[:upper:]' '[:lower:]')" + WORKDIR="$(mktemp -d)" + mkdir -p "${WORKDIR}/Sources/KernelsSmoke" + cat > "${WORKDIR}/Package.swift" < "${WORKDIR}/Sources/KernelsSmoke/main.swift" <<'SWIFT' + import Foundation + import ThreeLiveKernels + import ThreeLiveRuntime + + guard NSClassFromString("ThreeLiveNativeCompilerBackendRegistrar") == nil else { + fatalError("ThreeLiveCompilerNative unexpectedly linked into standalone consumer") + } + guard ThreeCompiler.backend == nil else { + fatalError("Expected ThreeCompiler.backend == nil without ThreeLiveCompiler") + } + guard ThreeLiveKernels.abiVersion == 1, ThreeLiveKernels.kernelCount == 24 else { + fatalError("Expected ThreeLiveKernels ABI v1 with 24 kernels") + } + print("Standalone ThreeLiveRuntime + ThreeLiveKernels consumer verified") + SWIFT + swift run --package-path "${WORKDIR}" + SMOKE_BIN="$(swift build --package-path "${WORKDIR}" --show-bin-path)/KernelsSmoke" + if nm "${SMOKE_BIN}" | grep -E '(_?three_compile_to_glb|boa_engine)'; then + echo "::error::Standalone ThreeLiveRuntime + ThreeLiveKernels binary leaked compiler/Boa symbols!" + exit 1 + fi + rm -rf "${WORKDIR}" + # `swift build` only covers the macOS host, where `canImport(UIKit)` is false and the # whole of QuickLookHelper.swift is preprocessed away. These steps are what actually # type-check the iOS-only and visionOS-only code paths. - - name: Compile for iOS + - name: Compile for iOS (ThreeLiveRuntime, ThreeLiveKernels, ThreeLiveCompiler) run: | + xcodebuild -scheme ThreeLiveRuntime \ + -destination 'generic/platform=iOS' \ + -derivedDataPath .build/xcodebuild-ios-runtime \ + build + xcodebuild -scheme ThreeLiveKernels \ + -destination 'generic/platform=iOS' \ + -derivedDataPath .build/xcodebuild-ios-kernels \ + build xcodebuild -scheme ThreeLiveCompiler \ -destination 'generic/platform=iOS' \ -derivedDataPath .build/xcodebuild-ios \ diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8ccfe14e..0143927c 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -50,6 +50,8 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + persist-credentials: false - name: Set up Node.js uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 @@ -70,24 +72,30 @@ jobs: - name: Build with Cargo env: CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER: rust-lld - run: cargo build --manifest-path compiler/Cargo.toml --release --bin three-live-cli --target ${{ matrix.target }} + TARGET: ${{ matrix.target }} + run: cargo build --manifest-path compiler/Cargo.toml --release --bin three-live-cli --target "${TARGET}" - name: Verify Linux binary is statically linked (zero GLIBC version dependency) if: runner.os == 'Linux' + env: + TARGET: ${{ matrix.target }} run: | - file "target/${{ matrix.target }}/release/three-live-cli" - if objdump -T "target/${{ matrix.target }}/release/three-live-cli" 2>/dev/null | grep -q "GLIBC_"; then + file "target/${TARGET}/release/three-live-cli" + if objdump -T "target/${TARGET}/release/three-live-cli" 2>/dev/null | grep -q "GLIBC_"; then echo "::error::Linux CLI binary contains dynamic GLIBC symbol references; expected static musl binary" exit 1 fi - name: Rename binary to target artifact name shell: bash + env: + TARGET: ${{ matrix.target }} + ARTIFACT_NAME: ${{ matrix.artifact_name }} run: | - if [ -f "target/${{ matrix.target }}/release/three-live-cli.exe" ]; then - cp "target/${{ matrix.target }}/release/three-live-cli.exe" "target/${{ matrix.target }}/release/${{ matrix.artifact_name }}" + if [ -f "target/${TARGET}/release/three-live-cli.exe" ]; then + cp "target/${TARGET}/release/three-live-cli.exe" "target/${TARGET}/release/${ARTIFACT_NAME}" else - cp "target/${{ matrix.target }}/release/three-live-cli" "target/${{ matrix.target }}/release/${{ matrix.artifact_name }}" + cp "target/${TARGET}/release/three-live-cli" "target/${TARGET}/release/${ARTIFACT_NAME}" fi - name: Upload Binary Artifact @@ -107,6 +115,8 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + persist-credentials: false - name: Set up Node.js uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 @@ -150,17 +160,50 @@ jobs: env: ANDROID_NDK_HOME: ${{ steps.setup-ndk.outputs.ndk-path }} - # Guard against shipping a Java-only AAR: without the cross-compiled .so files every - # ThreeCompiler call throws UnsatisfiedLinkError at runtime. - - name: Verify AAR contains native libraries + # Guard against shipping broken AARs: compiler and kernels AARs must carry all 3 ABI .so + # slices, kernels must carry zero boa_engine symbols, and runtime must carry zero .so files. + - name: Verify AARs contain expected native libraries and symbol isolation run: | + if cargo tree --manifest-path compiler/Cargo.toml -e normal --no-default-features --features kernels,jni | grep -q "boa_engine"; then + echo "::error::Boa-free kernels dependency graph includes boa_engine!" + exit 1 + fi + runtime_aar=$(ls mobile/android-runtime/build/outputs/aar/*-release.aar) + kernels_aar=$(ls mobile/android-kernels/build/outputs/aar/*-release.aar) aar=$(ls mobile/android/build/outputs/aar/*-release.aar) - echo "Inspecting ${aar}" + echo "Inspecting ${runtime_aar}, ${kernels_aar}, and ${aar}" + if unzip -l "${runtime_aar}" | grep -q "\.so$"; then + echo "::error::Runtime AAR (${runtime_aar}) must not bundle native .so libraries" + exit 1 + fi + tmp_check="$(mktemp -d)" + unzip -qo "${kernels_aar}" "jni/*" -d "${tmp_check}" for abi in arm64-v8a armeabi-v7a x86_64; do unzip -l "${aar}" | grep -q "jni/${abi}/libthree_live_compiler.so" \ - || { echo "::error::AAR is missing jni/${abi}/libthree_live_compiler.so"; exit 1; } + || { echo "::error::Compiler AAR is missing jni/${abi}/libthree_live_compiler.so"; exit 1; } + unzip -l "${kernels_aar}" | grep -q "jni/${abi}/libthree_live_kernels.so" \ + || { echo "::error::Kernels AAR is missing jni/${abi}/libthree_live_kernels.so"; exit 1; } + if nm -D "${tmp_check}/jni/${abi}/libthree_live_kernels.so" | grep -i "boa_engine"; then + echo "::error::Kernels AAR (${abi}) leaked boa_engine symbols!" + exit 1 + fi done - echo "All three ABIs present." + rm -rf "${tmp_check}" + echo "All three ABIs and Boa-free kernels symbol isolation verified." + + - name: Upload Android Runtime AAR Artifact + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 + with: + name: three-live-runtime-release-aar + path: mobile/android-runtime/build/outputs/aar/*-release.aar + if-no-files-found: error + + - name: Upload Android Kernels AAR Artifact + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 + with: + name: three-live-kernels-release-aar + path: mobile/android-kernels/build/outputs/aar/*-release.aar + if-no-files-found: error # if-no-files-found defaults to 'warn', which previously let this job go green while # uploading nothing at all. @@ -222,6 +265,8 @@ jobs: - name: Publish Android AARs to Maven Local run: > gradle + :mobile:android-runtime:publishReleasePublicationToMavenLocal + :mobile:android-kernels:publishReleasePublicationToMavenLocal :mobile:android:publishReleasePublicationToMavenLocal :mobile:android-filament:publishReleasePublicationToMavenLocal :mobile:android-firebase-ai:publishReleasePublicationToMavenLocal @@ -237,6 +282,8 @@ jobs: GITHUB_REPOSITORY: ${{ github.repository }} run: | gradle \ + :mobile:android-runtime:publishReleasePublicationToGitHubPackagesRepository \ + :mobile:android-kernels:publishReleasePublicationToGitHubPackagesRepository \ :mobile:android:publishReleasePublicationToGitHubPackagesRepository \ :mobile:android-filament:publishReleasePublicationToGitHubPackagesRepository \ :mobile:android-firebase-ai:publishReleasePublicationToGitHubPackagesRepository \ @@ -258,6 +305,8 @@ jobs: exit 0 fi gradle \ + :mobile:android-runtime:publishReleasePublicationToMavenCentralRepository \ + :mobile:android-kernels:publishReleasePublicationToMavenCentralRepository \ :mobile:android:publishReleasePublicationToMavenCentralRepository \ :mobile:android-filament:publishReleasePublicationToMavenCentralRepository \ :mobile:android-firebase-ai:publishReleasePublicationToMavenCentralRepository \ @@ -267,14 +316,17 @@ jobs: --stacktrace build-ios: - name: Build Apple XCFramework + name: Build Apple XCFrameworks if: github.event_name == 'push' runs-on: macos-latest outputs: checksum: ${{ steps.package_xcframework.outputs.checksum }} + kernels_checksum: ${{ steps.package_xcframework.outputs.kernels_checksum }} steps: - name: Checkout repository uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + persist-credentials: false - name: Set up Node.js uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 @@ -292,28 +344,41 @@ jobs: toolchain: stable targets: aarch64-apple-ios, aarch64-apple-ios-sim, x86_64-apple-ios, aarch64-apple-darwin, x86_64-apple-darwin - - name: Build XCFramework + - name: Build XCFrameworks (ThreeLiveKernels & ThreeLiveCompiler) run: bash scripts/build_ios_xcframework.sh - - name: Verify XCFramework completeness - run: bash scripts/verify_xcframework.sh mobile/ios/ThreeLiveCompiler.xcframework + - name: Verify XCFrameworks completeness & Boa symbol isolation + run: | + bash scripts/verify_xcframework.sh --kernels mobile/ios/ThreeLiveKernels.xcframework + bash scripts/verify_xcframework.sh mobile/ios/ThreeLiveCompiler.xcframework - - name: Zip XCFramework & Compute Checksum + - name: Zip XCFrameworks & Compute Checksums id: package_xcframework run: | cd mobile/ios zip -r -y ThreeLiveCompiler.xcframework.zip ThreeLiveCompiler.xcframework + zip -r -y ThreeLiveKernels.xcframework.zip ThreeLiveKernels.xcframework CHECKSUM=$(swift package compute-checksum ThreeLiveCompiler.xcframework.zip) - echo "XCFramework SHA-256 Checksum: ${CHECKSUM}" + KERNELS_CHECKSUM=$(swift package compute-checksum ThreeLiveKernels.xcframework.zip) + echo "ThreeLiveCompiler.xcframework SHA-256 Checksum: ${CHECKSUM}" + echo "ThreeLiveKernels.xcframework SHA-256 Checksum: ${KERNELS_CHECKSUM}" echo "checksum=${CHECKSUM}" >> "$GITHUB_OUTPUT" + echo "kernels_checksum=${KERNELS_CHECKSUM}" >> "$GITHUB_OUTPUT" - - name: Upload iOS XCFramework Artifact + - name: Upload ThreeLiveCompiler XCFramework Artifact uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 with: name: ThreeLiveCompiler.xcframework.zip path: mobile/ios/ThreeLiveCompiler.xcframework.zip if-no-files-found: error + - name: Upload ThreeLiveKernels XCFramework Artifact + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 + with: + name: ThreeLiveKernels.xcframework.zip + path: mobile/ios/ThreeLiveKernels.xcframework.zip + if-no-files-found: error + github-release: name: Create GitHub Release @@ -324,6 +389,8 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + persist-credentials: false - name: Set up Node.js uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 @@ -344,11 +411,11 @@ jobs: run: | mkdir -p dist-release web-bundle/three-live web-bundle/xr-live find release-artifacts -type f -exec cp {} dist-release/ \; - cp dist/*.js dist/*.min.js dist/three-live-sandbox.html web-bundle/ - cp three-live/dist/*.js three-live/dist/*.min.js three-live/dist/*.cjs three-live/dist/*.d.ts web-bundle/three-live/ + cp dist/*.js dist/*.min.js dist/engine-requirements.json dist/three-live-sandbox.html web-bundle/ + cp three-live/dist/*.js three-live/dist/*.min.js three-live/dist/*.cjs three-live/dist/*.d.ts three-live/dist/engine-requirements.json web-bundle/three-live/ cp xr-live/dist/*.js xr-live/dist/*.min.js xr-live/dist/*.cjs xr-live/dist/*.d.ts web-bundle/xr-live/ (cd web-bundle && zip -r ../dist-release/three-live-web-dist.zip .) - cp dist/three-live.min.js dist/three-live-viewer.min.js dist/three-live-studio.min.js dist/three-live-cad.min.js dist/three-live-animation.min.js dist/three-live-sandbox.html dist-release/ + cp dist/three-live.min.js dist/three-live-viewer.min.js dist/three-live-studio.min.js dist/three-live-cad.min.js dist/three-live-animation.min.js dist/three-live-compiler-bundle.min.js dist/engine-requirements.json dist/three-live-sandbox.html dist-release/ cp three-live/dist/three-live-full.min.js dist-release/ cp xr-live/dist/xr-live.min.js xr-live/dist/xr-live-viewer.min.js xr-live/dist/xr-live-sensors.min.js xr-live/dist/xr-live-spatial.min.js xr-live/dist/xr-live-sim.min.js xr-live/dist/xr-live-webgpu.min.js xr-live/dist/xr-live-full.min.js dist-release/ ls -la dist-release @@ -363,6 +430,7 @@ jobs: publish-npm: name: Publish NPM Packages + if: github.event_name == 'push' runs-on: ubuntu-latest permissions: contents: read @@ -370,6 +438,8 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + persist-credentials: false - name: Set up Node.js uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 @@ -416,6 +486,8 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + persist-credentials: false - name: Install mcp-publisher and publish server.json via GitHub OIDC run: | @@ -426,10 +498,13 @@ jobs: publish-crates: name: Publish Crates.io + if: github.event_name == 'push' runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + persist-credentials: false - name: Set up Node.js uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 @@ -476,16 +551,18 @@ jobs: ref: ${{ github.ref_name }} fetch-depth: 0 - - name: Pin release tag and checksum into Package.swift + - name: Pin release tag and checksums into Package.swift env: TAG: ${{ github.ref_name }} CHECKSUM: ${{ needs.build-ios.outputs.checksum }} - run: bash scripts/update_spm_binary_target.sh "${TAG}" "${CHECKSUM}" + KERNELS_CHECKSUM: ${{ needs.build-ios.outputs.kernels_checksum }} + run: bash scripts/update_spm_binary_target.sh "${TAG}" "${CHECKSUM}" "${KERNELS_CHECKSUM}" - name: Commit, move tag, and update main env: TAG: ${{ github.ref_name }} CHECKSUM: ${{ needs.build-ios.outputs.checksum }} + KERNELS_CHECKSUM: ${{ needs.build-ios.outputs.kernels_checksum }} run: | set -euo pipefail if git diff --quiet -- Package.swift; then @@ -495,17 +572,17 @@ jobs: git config user.name "github-actions[bot]" git config user.email "41898282+github-actions[bot]@users.noreply.github.com" git add Package.swift - git commit -m "chore: pin ThreeLiveCompiler.xcframework checksum for ${TAG}" + git commit -m "chore: pin ThreeLiveCompiler and ThreeLiveKernels XCFramework checksums for ${TAG}" git tag -f "${TAG}" git push --force origin "refs/tags/${TAG}" - # Also update Package.swift on main so main carries the latest release checksum + # Also update Package.swift on main so main carries the latest release checksums git fetch origin main git checkout -B main origin/main - bash scripts/update_spm_binary_target.sh "${TAG}" "${CHECKSUM}" + bash scripts/update_spm_binary_target.sh "${TAG}" "${CHECKSUM}" "${KERNELS_CHECKSUM}" if ! git diff --quiet -- Package.swift; then git add Package.swift - git commit -m "chore: pin ThreeLiveCompiler.xcframework checksum for ${TAG} [skip ci]" + git commit -m "chore: pin ThreeLiveCompiler and ThreeLiveKernels XCFramework checksums for ${TAG} [skip ci]" git push origin HEAD:refs/heads/main || echo "::warning::Could not push Package.swift update to main" fi @@ -536,6 +613,11 @@ jobs: cat > Sources/ConsumerProbe/main.swift <<'SWIFT' import Foundation import ThreeLiveCompiler + import ThreeLiveKernels + + guard ThreeLiveKernels.abiVersion == 1, ThreeLiveKernels.kernelCount == 24 else { + fatalError("Resolved ThreeLiveKernels did not expose expected ABI v1 descriptor table") + } let script = """ const mesh = new THREE.Mesh( @@ -549,7 +631,7 @@ jobs: guard Array(glb.prefix(4)) == [0x67, 0x6C, 0x54, 0x46] else { fatalError("Resolved package did not produce a valid GLB container") } - print("Consumed ThreeLiveCompiler successfully: \(glb.count) bytes of GLB") + print("Consumed ThreeLiveCompiler + ThreeLiveKernels successfully: \(glb.count) bytes of GLB") SWIFT cat > Package.swift < | Goal | Element | Recommended Bundle | Enforced Budget | | :--- | :--- | :--- | :---: | -| `.glb` / `.usdz` 3D viewer + 1-tap AR handoff (no scripting) | `` | `three-live-viewer.min.js` | `< 34 KB gz` | -| Procedural ` "#; - let (_, ok_diags) = validate_scene_source(ok_html, "ok.html"); - assert!( - !ok_diags.iter().any(|d| d.code == "TL401" || d.code == "TL403"), - "Expected zero TL401/TL403 diagnostics when capabilities are granted, got: {:?}", - ok_diags - ); + let (_, ok_diags) = validate_scene_source(ok_html, "ok.html"); + assert!( + !ok_diags.iter().any(|d| d.code == "TL401" || d.code == "TL403"), + "Expected zero TL401/TL403 diagnostics when capabilities are granted, got: {:?}", + ok_diags + ); - let missing_html = r#" + let missing_html = r#" "#; - let (_, diags) = validate_scene_source(missing_html, "missing.html"); - assert!(!diags.iter().any(|d| d.code == "TL401")); - let tl403: Vec<&str> = - diags.iter().filter(|d| d.code == "TL403").map(|d| d.message.as_str()).collect(); - assert!(tl403.iter().any(|m| m.contains("xr-hands"))); - assert!(tl403.iter().any(|m| m.contains("shared-room"))); - assert!(tl403.iter().any(|m| m.contains("persistent-anchors"))); - assert!(tl403.iter().any(|m| m.contains("userData.xr.splatUrl"))); - assert!(tl403.iter().any(|m| m.contains("userData.xr.audio.src"))); - assert!(tl403.iter().any(|m| m.contains("userData.xr.holdToAsk"))); - - // Audit regressions: `texture-pack`, `xr-mode="vr"`, `allow-client-scripts` + `-scripts` - // negation, and multi-element HTML capability isolation. - let lockdown_html = r#" + let (_, diags) = validate_scene_source(missing_html, "missing.html"); + assert!(!diags.iter().any(|d| d.code == "TL401")); + let tl403: Vec<&str> = + diags.iter().filter(|d| d.code == "TL403").map(|d| d.message.as_str()).collect(); + assert!(tl403.iter().any(|m| m.contains("xr-hands"))); + assert!(tl403.iter().any(|m| m.contains("shared-room"))); + assert!(tl403.iter().any(|m| m.contains("persistent-anchors"))); + assert!(tl403.iter().any(|m| m.contains("userData.xr.splatUrl"))); + assert!(tl403.iter().any(|m| m.contains("userData.xr.audio.src"))); + assert!(tl403.iter().any(|m| m.contains("userData.xr.holdToAsk"))); + + // Audit regressions: `texture-pack`, `xr-mode="vr"`, `allow-client-scripts` + + // `-scripts` negation, and multi-element HTML capability isolation. + let lockdown_html = r#" "#; - let (_, lock_diags) = validate_scene_source(lockdown_html, "lockdown.html"); - assert!( - lock_diags.iter().any(|d| d.code == "TL401"), - "capabilities=\"none\" must override allow-client-scripts and emit TL401" - ); - assert!(lock_diags.iter().any(|d| d.code == "TL403" && d.message.contains("texture-pack"))); - assert!(lock_diags - .iter() - .any(|d| d.code == "TL403" && d.message.contains("xr-mode=\"vr\""))); + let (_, lock_diags) = validate_scene_source(lockdown_html, "lockdown.html"); + assert!( + lock_diags.iter().any(|d| d.code == "TL401"), + "capabilities=\"none\" must override allow-client-scripts and emit TL401" + ); + assert!(lock_diags + .iter() + .any(|d| d.code == "TL403" && d.message.contains("texture-pack"))); + assert!(lock_diags + .iter() + .any(|d| d.code == "TL403" && d.message.contains("xr-mode=\"vr\""))); - let multi_html = r#" + let multi_html = r#" "#; - let (_, multi_diags) = validate_scene_source(multi_html, "multi.html"); - assert!( + let (_, multi_diags) = validate_scene_source(multi_html, "multi.html"); + assert!( multi_diags .iter() .any(|d| d.code == "TL403" && d.message.contains("RestrictedSplat")), "Restricted sibling element must still receive TL403 even when another element has capabilities=\"all\"" ); - assert!(!multi_diags.iter().any(|d| d.code == "TL403" && d.message.contains("AllowedBox"))); + assert!(!multi_diags + .iter() + .any(|d| d.code == "TL403" && d.message.contains("AllowedBox"))); + } } } + +#[cfg(feature = "boa")] +pub use html_lint::*; diff --git a/compiler/src/material_preset.rs b/compiler/src/material_preset.rs index fcfd0de3..aa9c6ade 100644 --- a/compiler/src/material_preset.rs +++ b/compiler/src/material_preset.rs @@ -19,7 +19,9 @@ //! operation order, so their RGBA output is byte-identical across Web workers (V8), the Rust //! CLI / MCP server, Android, and iOS (Boa). Keep the two in lockstep. +#[cfg(feature = "boa")] use boa_engine::object::builtins::JsArray; +#[cfg(feature = "boa")] use boa_engine::{js_string, Context, JsResult, JsValue, NativeFunction}; /// Preset names in kernel-index order (shared with the JS side). @@ -414,6 +416,7 @@ pub(crate) fn unit_to_byte(v: f64) -> u8 { to_byte(v) } +#[cfg(feature = "boa")] fn arg_f64(args: &[JsValue], i: usize, ctx: &mut Context) -> JsResult { Ok(args.get(i).map(|v| v.to_number(ctx)).transpose()?.unwrap_or(0.0)) } @@ -421,6 +424,7 @@ fn arg_f64(args: &[JsValue], i: usize, ctx: &mut Context) -> JsResult { /// `__three_live_native_material_preset__(kind, size, scale, seed, r, g, b, r2, g2, b2, /// roughness, normalStrength)` -> `[albedo, normal, orm]` RGBA8 `Uint8Array`s, or `null` when /// the parameters are out of range (the JS caller then uses its own kernels). +#[cfg(feature = "boa")] pub fn native_material_preset( _this: &JsValue, args: &[JsValue], @@ -465,6 +469,7 @@ pub fn native_material_preset( } /// Registers `__three_live_native_material_preset__` on the global object. +#[cfg(feature = "boa")] pub fn register_native_material_callables(context: &mut Context) -> JsResult<()> { context.register_global_builtin_callable( js_string!("__three_live_native_material_preset__"), diff --git a/compiler/src/reference.rs b/compiler/src/reference.rs index 6307ee8a..d5c1ee68 100644 --- a/compiler/src/reference.rs +++ b/compiler/src/reference.rs @@ -172,244 +172,9 @@ fn check_dims(w: u64, h: u64) -> Result<(), String> { // --------------------------------------------------------------------------------------------- pub(crate) fn decode_png(png: &[u8]) -> Result { - let bad = |m: &str| format!("Invalid PNG reference: {}", m); - let mut pos = 8usize; - let mut ihdr: Option<(u32, u32, u8, u8, u8)> = None; - let mut idat = Vec::new(); - let mut plte: Vec<[u8; 3]> = Vec::new(); - let mut trns: Vec = Vec::new(); - while pos + 12 <= png.len() { - let len = u32::from_be_bytes([png[pos], png[pos + 1], png[pos + 2], png[pos + 3]]) as usize; - let ctype = &png[pos + 4..pos + 8]; - let start = pos + 8; - let end = start.checked_add(len).ok_or_else(|| bad("chunk length overflow"))?; - if end + 4 > png.len() { - return Err(bad("truncated chunk")); - } - let data = &png[start..end]; - match ctype { - b"IHDR" => { - if len != 13 { - return Err(bad("IHDR length")); - } - let w = u32::from_be_bytes([data[0], data[1], data[2], data[3]]); - let h = u32::from_be_bytes([data[4], data[5], data[6], data[7]]); - ihdr = Some((w, h, data[8], data[9], data[12])); - } - b"PLTE" => { - plte = data.chunks_exact(3).map(|c| [c[0], c[1], c[2]]).collect(); - } - b"tRNS" => trns = data.to_vec(), - b"IDAT" => { - if idat.len() + data.len() > MAX_REFERENCE_BYTES { - return Err(bad("IDAT too large")); - } - idat.extend_from_slice(data); - } - b"IEND" => break, - _ => {} - } - pos = end + 4; - } - let (w, h, depth, ctype, interlace) = ihdr.ok_or_else(|| bad("missing IHDR"))?; - check_dims(w as u64, h as u64)?; - let channels: usize = match ctype { - 0 => 1, - 2 => 3, - 3 => 1, - 4 => 2, - 6 => 4, - _ => return Err(bad("unknown color type")), - }; - let depth_ok = match ctype { - 0 => matches!(depth, 1 | 2 | 4 | 8 | 16), - 3 => matches!(depth, 1 | 2 | 4 | 8), - _ => matches!(depth, 8 | 16), - }; - if !depth_ok { - return Err(bad("invalid bit depth for color type")); - } - if ctype == 3 && plte.is_empty() { - return Err(bad("palette image without PLTE")); - } - if interlace > 1 { - return Err(bad("unknown interlace method")); - } - let (wu, hu) = (w as usize, h as usize); - let bits_pp = channels * depth as usize; - let bpp = bits_pp.div_ceil(8).max(1); - let passes: Vec<(usize, usize, usize, usize)> = if interlace == 1 { - vec![ - (0, 0, 8, 8), - (4, 0, 8, 8), - (0, 4, 4, 8), - (2, 0, 4, 4), - (0, 2, 2, 4), - (1, 0, 2, 2), - (0, 1, 1, 2), - ] - } else { - vec![(0, 0, 1, 1)] - }; - let pass_dims: Vec<(usize, usize)> = passes - .iter() - .map(|&(x0, y0, dx, dy)| { - let pw = if wu > x0 { (wu - x0).div_ceil(dx) } else { 0 }; - let ph = if hu > y0 { (hu - y0).div_ceil(dy) } else { 0 }; - (pw, ph) - }) - .collect(); - let expected: usize = pass_dims - .iter() - .filter(|(pw, ph)| *pw > 0 && *ph > 0) - .map(|&(pw, ph)| ph * (1 + (pw * bits_pp).div_ceil(8))) - .sum(); - let raw = miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(&idat, expected + 4096) - .map_err(|_| bad("corrupt or oversized zlib stream"))?; - if raw.len() < expected { - return Err(bad("image data is truncated")); - } - - let max_v = (1u32 << depth) - 1; - let mut out = vec![0u8; wu * hu * 4]; - let mut off = 0usize; - for (pi, &(x0, y0, dx, dy)) in passes.iter().enumerate() { - let (pw, ph) = pass_dims[pi]; - if pw == 0 || ph == 0 { - continue; - } - let row_len = (pw * bits_pp).div_ceil(8); - let mut prev = vec![0u8; row_len]; - let mut cur = vec![0u8; row_len]; - for ry in 0..ph { - let ft = raw[off]; - cur.copy_from_slice(&raw[off + 1..off + 1 + row_len]); - off += 1 + row_len; - unfilter_row(ft, &mut cur, &prev, bpp).map_err(|e| bad(&e))?; - for rx in 0..pw { - let px = convert_png_pixel(&cur, rx, ctype, depth, channels, max_v, &plte, &trns); - let (x, y) = (x0 + rx * dx, y0 + ry * dy); - let o = (y * wu + x) * 4; - out[o..o + 4].copy_from_slice(&px); - } - std::mem::swap(&mut prev, &mut cur); - } - } - Ok(ReferenceImage { width: w, height: h, rgba: out }) -} - -fn unfilter_row(ft: u8, cur: &mut [u8], prev: &[u8], bpp: usize) -> Result<(), String> { - match ft { - 0 => {} - 1 => { - for i in bpp..cur.len() { - cur[i] = cur[i].wrapping_add(cur[i - bpp]); - } - } - 2 => { - for i in 0..cur.len() { - cur[i] = cur[i].wrapping_add(prev[i]); - } - } - 3 => { - for i in 0..cur.len() { - let a = if i >= bpp { cur[i - bpp] as u16 } else { 0 }; - cur[i] = cur[i].wrapping_add(((a + prev[i] as u16) / 2) as u8); - } - } - 4 => { - for i in 0..cur.len() { - let a = if i >= bpp { cur[i - bpp] as i16 } else { 0 }; - let b = prev[i] as i16; - let c = if i >= bpp { prev[i - bpp] as i16 } else { 0 }; - let p = a + b - c; - let (pa, pb, pc) = ((p - a).abs(), (p - b).abs(), (p - c).abs()); - let pred = if pa <= pb && pa <= pc { - a - } else if pb <= pc { - b - } else { - c - }; - cur[i] = cur[i].wrapping_add(pred as u8); - } - } - _ => return Err(format!("unknown filter type {}", ft)), - } - Ok(()) -} - -/// Reads sample `idx` (in samples, not bytes) from a scanline at `depth` bits per sample. -#[inline] -fn read_sample(row: &[u8], idx: usize, depth: u8) -> u32 { - match depth { - 8 => row[idx] as u32, - 16 => ((row[idx * 2] as u32) << 8) | row[idx * 2 + 1] as u32, - _ => { - let bit = idx * depth as usize; - let byte = row[bit / 8]; - let shift = 8 - depth as usize - (bit % 8); - ((byte >> shift) as u32) & ((1u32 << depth) - 1) - } - } -} - -#[allow(clippy::too_many_arguments)] -fn convert_png_pixel( - row: &[u8], - x: usize, - ctype: u8, - depth: u8, - channels: usize, - max_v: u32, - plte: &[[u8; 3]], - trns: &[u8], -) -> [u8; 4] { - let to8 = |v: u32| -> u8 { - if depth == 16 { - (v >> 8) as u8 - } else if depth == 8 { - v as u8 - } else { - ((v * 255 + max_v / 2) / max_v) as u8 - } - }; - let base = x * channels; - match ctype { - 0 => { - let v = read_sample(row, base, depth); - let key = (trns.len() >= 2).then(|| u16::from_be_bytes([trns[0], trns[1]]) as u32); - let g = to8(v); - [g, g, g, if key == Some(v) { 0 } else { 255 }] - } - 2 => { - let (r, g, b) = ( - read_sample(row, base, depth), - read_sample(row, base + 1, depth), - read_sample(row, base + 2, depth), - ); - let keyed = trns.len() >= 6 - && r == u16::from_be_bytes([trns[0], trns[1]]) as u32 - && g == u16::from_be_bytes([trns[2], trns[3]]) as u32 - && b == u16::from_be_bytes([trns[4], trns[5]]) as u32; - [to8(r), to8(g), to8(b), if keyed { 0 } else { 255 }] - } - 3 => { - let i = read_sample(row, base, depth) as usize; - let c = plte.get(i).copied().unwrap_or([0, 0, 0]); - [c[0], c[1], c[2], trns.get(i).copied().unwrap_or(255)] - } - 4 => { - let g = to8(read_sample(row, base, depth)); - [g, g, g, to8(read_sample(row, base + 1, depth))] - } - _ => [ - to8(read_sample(row, base, depth)), - to8(read_sample(row, base + 1, depth)), - to8(read_sample(row, base + 2, depth)), - to8(read_sample(row, base + 3, depth)), - ], - } + let (width, height, rgba) = crate::kernels::decode_png_rgba(png)?; + check_dims(width as u64, height as u64)?; + Ok(ReferenceImage { width, height, rgba }) } // --------------------------------------------------------------------------------------------- diff --git a/compiler/src/render.rs b/compiler/src/render.rs index fa5ac980..021d6171 100644 --- a/compiler/src/render.rs +++ b/compiler/src/render.rs @@ -21,8 +21,6 @@ use crate::spatial_lint::{ mat4_look_at, mat4_mul, mat4_perspective, mat4_transform_point, mat4_transform_vec4, WorldAABB, }; use crate::usdz::calculate_crc32; -use std::cell::RefCell; -use std::sync::Arc; /// Camera / viewport selection for headless rendering. #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -2022,173 +2020,14 @@ pub(crate) fn draw_label_badge( /// Encodes an RGBA8 buffer as a zlib-compressed PNG with per-row adaptive filtering /// (minimum-sum-of-absolute-differences heuristic). Produces far smaller files than /// [`encode_png_rgba`] for large composites such as reference comparison sheets. +pub use crate::kernels::{ + compress_png_data_uri, encode_png_rgba_level, recompress_stored_png, TEXTURE_PNG_LEVEL, +}; + pub fn encode_png_rgba_compressed(width: u32, height: u32, rgba: &[u8]) -> Result, String> { encode_png_rgba_level(width, height, rgba, 6) } -/// [`encode_png_rgba_compressed`] at an explicit `miniz_oxide` level (1 = fastest, 9 = smallest). -pub fn encode_png_rgba_level( - width: u32, - height: u32, - rgba: &[u8], - level: u8, -) -> Result, String> { - if width == 0 || height == 0 { - return Err("PNG dimensions must be >= 1x1".to_string()); - } - let w = width as usize; - let h = height as usize; - let row_bytes = w.checked_mul(4).ok_or("PNG width overflow")?; - let needed = row_bytes.checked_mul(h).ok_or("PNG size overflow")?; - if rgba.len() < needed { - return Err(format!( - "RGBA buffer too small for {}x{} PNG ({} < {})", - w, - h, - rgba.len(), - needed - )); - } - let mut raw = Vec::with_capacity(h * (1 + row_bytes)); - let mut cand = vec![0u8; row_bytes]; - let mut best = vec![0u8; row_bytes]; - for y in 0..h { - let cur = &rgba[y * row_bytes..(y + 1) * row_bytes]; - let prev = if y > 0 { Some(&rgba[(y - 1) * row_bytes..y * row_bytes]) } else { None }; - let mut best_ft = 0u8; - let mut best_score = u64::MAX; - for ft in 0u8..5 { - if ft >= 2 && prev.is_none() && ft != 4 { - continue; - } - let mut score = 0u64; - for i in 0..row_bytes { - let a = if i >= 4 { cur[i - 4] as i16 } else { 0 }; - let b = prev.map(|p| p[i] as i16).unwrap_or(0); - let c = if i >= 4 { prev.map(|p| p[i - 4] as i16).unwrap_or(0) } else { 0 }; - let pred = match ft { - 0 => 0, - 1 => a, - 2 => b, - 3 => (a + b) / 2, - _ => { - let pp = a + b - c; - let (pa, pb, pc) = ((pp - a).abs(), (pp - b).abs(), (pp - c).abs()); - if pa <= pb && pa <= pc { - a - } else if pb <= pc { - b - } else { - c - } - } - }; - let v = (cur[i] as i16 - pred) as u8; - cand[i] = v; - score += (v as i8).unsigned_abs() as u64; - } - if score < best_score { - best_score = score; - best_ft = ft; - best.copy_from_slice(&cand); - } - } - raw.push(best_ft); - raw.extend_from_slice(&best); - } - let zlib = miniz_oxide::deflate::compress_to_vec_zlib(&raw, level.clamp(1, 9)); - - let mut png = Vec::with_capacity(64 + zlib.len()); - png.extend_from_slice(b"\x89PNG\r\n\x1a\n"); - let write_chunk = |out: &mut Vec, chunk_type: &[u8; 4], data: &[u8]| { - out.extend_from_slice(&(data.len() as u32).to_be_bytes()); - let crc_start = out.len(); - out.extend_from_slice(chunk_type); - out.extend_from_slice(data); - let crc = calculate_crc32(&out[crc_start..]); - out.extend_from_slice(&crc.to_be_bytes()); - }; - let mut ihdr = [0u8; 13]; - ihdr[0..4].copy_from_slice(&width.to_be_bytes()); - ihdr[4..8].copy_from_slice(&height.to_be_bytes()); - ihdr[8] = 8; - ihdr[9] = 6; - write_chunk(&mut png, b"IHDR", &ihdr); - write_chunk(&mut png, b"IDAT", &zlib); - write_chunk(&mut png, b"IEND", &[]); - Ok(png) -} - -/// `miniz_oxide` level for texture PNGs written into GLB files: close to level 6 in size on -/// generated textures at a fraction of the time. -pub const TEXTURE_PNG_LEVEL: u8 = 3; - -/// Re-encodes an 8-bit RGBA PNG whose zlib stream starts with a stored (uncompressed) DEFLATE -/// block — what the in-engine texture encoders emit, since they run inside the script -/// execution ceiling — with adaptive filtering and DEFLATE. Returns `None` when the PNG is -/// already compressed, is not 8-bit RGBA, or would not shrink. -pub fn recompress_stored_png(png: &[u8]) -> Option> { - if png.len() < 33 || &png[0..8] != b"\x89PNG\r\n\x1a\n" || &png[12..16] != b"IHDR" { - return None; - } - // IHDR: bit depth 8, color type 6 (RGBA), no interlace. - if png[24] != 8 || png[25] != 6 || png[28] != 0 { - return None; - } - // First IDAT: zlib header (2 bytes), then the first DEFLATE block header (BTYPE bits 1-2). - let mut pos = 8usize; - let mut first_block = None; - while pos + 12 <= png.len() { - let len = u32::from_be_bytes([png[pos], png[pos + 1], png[pos + 2], png[pos + 3]]) as usize; - if &png[pos + 4..pos + 8] == b"IDAT" { - if len >= 3 && pos + 8 + 3 <= png.len() { - first_block = Some(png[pos + 10]); - } - break; - } - pos = pos.checked_add(12 + len)?; - } - if (first_block? >> 1) & 3 != 0 { - return None; - } - let img = crate::reference::decode_png(png).ok()?; - let out = encode_png_rgba_level(img.width, img.height, &img.rgba, TEXTURE_PNG_LEVEL).ok()?; - (out.len() < png.len()).then_some(out) -} - -thread_local! { - // (FNV-1a 64 of the URI, URI length) → recompressed URI. Live previews re-export the - // same textures on every edit; this keeps that from re-running DEFLATE each time. - static PNG_URI_CACHE: RefCell)>> = const { RefCell::new(Vec::new()) }; -} -const PNG_URI_CACHE_ENTRIES: usize = 24; - -/// [`recompress_stored_png`] for a `data:image/png;base64,...` URI (cached per thread). -/// Returns the input unchanged when it is not a stored PNG data URI. -pub fn compress_png_data_uri(uri: &str) -> Arc { - let Some(b64) = uri.strip_prefix("data:image/png;base64,") else { - return Arc::from(uri); - }; - let id = (crate::ai_texture::fnv1a64(uri.as_bytes()), uri.len()); - if let Some(hit) = - PNG_URI_CACHE.with(|c| c.borrow().iter().find(|(k, _)| *k == id).map(|(_, v)| v.clone())) - { - return hit; - } - let out: Arc = decode_base64_str(b64, 17 * 1024 * 1024) - .and_then(|png| recompress_stored_png(&png)) - .map(|png| Arc::from(format!("data:image/png;base64,{}", crate::mcp::encode_base64(&png)))) - .unwrap_or_else(|| Arc::from(uri)); - PNG_URI_CACHE.with(|c| { - let mut c = c.borrow_mut(); - if c.len() >= PNG_URI_CACHE_ENTRIES { - c.remove(0); - } - c.push((id, out.clone())); - }); - out -} - pub fn encode_png_rgba(width: u32, height: u32, rgba: &[u8]) -> Result, String> { if width == 0 || height == 0 { return Err("PNG dimensions must be >= 1x1".to_string()); diff --git a/compiler/src/usdz.rs b/compiler/src/usdz.rs index cd5844e8..1d6abb0b 100644 --- a/compiler/src/usdz.rs +++ b/compiler/src/usdz.rs @@ -18,6 +18,7 @@ //! and 64-byte payload-aligned uncompressed ZIP archiving for Apple RealityKit. use crate::constants::SECURITY_3D_MAX_HEAP_BYTES; +#[cfg(feature = "boa")] use boa_engine::{ js_string, object::builtins::{JsArray, JsArrayBuffer, JsTypedArray, JsUint8Array}, @@ -31,6 +32,7 @@ use boa_engine::{ /// these hooks are installed as frozen, non-deletable globals, a script could hand /// one an arbitrarily large buffer and force a proportional Rust allocation — and a /// Rust allocation failure aborts the process, which `catch_unwind` cannot contain. +#[cfg(feature = "boa")] #[inline] fn check_native_input_size(len: usize, hook: &str) -> JsResult<()> { if len > SECURITY_3D_MAX_HEAP_BYTES { @@ -140,13 +142,18 @@ pub struct ZipEntryRef<'a> { pub data: &'a [u8], } +#[cfg(feature = "boa")] thread_local! { static CAPTURE_USDZ_DIRECT: std::cell::Cell = const { std::cell::Cell::new(false) }; static STASHED_USDZ_BYTES: std::cell::RefCell>> = const { std::cell::RefCell::new(None) }; + static CAPTURE_GLB_DIRECT: std::cell::Cell = const { std::cell::Cell::new(false) }; + static STASHED_GLB_BYTES: std::cell::RefCell>> = const { std::cell::RefCell::new(None) }; } +#[cfg(feature = "boa")] struct DirectCaptureGuard; +#[cfg(feature = "boa")] impl Drop for DirectCaptureGuard { fn drop(&mut self) { CAPTURE_USDZ_DIRECT.with(|c| c.set(false)); @@ -158,6 +165,7 @@ impl Drop for DirectCaptureGuard { /// Runs a closure with direct Rust USDZ buffer capture enabled, bypassing the Boa JS heap /// round-trip. +#[cfg(feature = "boa")] pub fn with_direct_usdz_capture(f: F) -> (R, Option>) where F: FnOnce() -> R, @@ -170,8 +178,37 @@ where (res, captured) } +#[cfg(feature = "boa")] +struct DirectGlbCaptureGuard; + +#[cfg(feature = "boa")] +impl Drop for DirectGlbCaptureGuard { + fn drop(&mut self) { + CAPTURE_GLB_DIRECT.with(|c| c.set(false)); + STASHED_GLB_BYTES.with(|s| { + let _ = s.borrow_mut().take(); + }); + } +} + +/// Runs a closure with direct Rust GLB buffer capture enabled, capturing the `Uint8Array` +/// emitted by `sceneToGLB` without returning or re-extracting a JS TypedArray value. +#[cfg(feature = "boa")] +pub fn with_direct_glb_capture(f: F) -> (R, Option>) +where + F: FnOnce() -> R, +{ + let _guard = DirectGlbCaptureGuard; + CAPTURE_GLB_DIRECT.with(|c| c.set(true)); + STASHED_GLB_BYTES.with(|s| *s.borrow_mut() = None); + let res = f(); + let captured = STASHED_GLB_BYTES.with(|s| s.borrow_mut().take()); + (res, captured) +} + /// Creates a Boa `JsUint8Array` from a byte slice with a single exact-sized 64-byte aligned /// allocation, avoiding `JsUint8Array::from_iter`'s `FlatMap` `size_hint() == 0` reallocation loop. +#[cfg(feature = "boa")] pub fn js_uint8_array_from_slice(slice: &[u8], context: &mut Context) -> JsResult { let mut aligned = boa_engine::object::builtins::AlignedVec::with_capacity(64, slice.len()); aligned.extend_from_slice(slice); @@ -364,6 +401,7 @@ pub fn build_usdz_zip(entries: &[ZipEntry]) -> Vec { } /// Helper to extract binary buffer bytes from either a TypedArray, ArrayBuffer, or Array. +#[cfg(feature = "boa")] pub fn extract_byte_buffer(val: &JsValue, context: &mut Context) -> Result, String> { if let Some(obj) = val.as_object() { if let Ok(typed_arr) = JsTypedArray::from_object(obj.clone()) { @@ -422,6 +460,7 @@ pub fn extract_byte_buffer(val: &JsValue, context: &mut Context) -> Result JsResult { if let Some(val) = args.first() { if let Some(obj) = val.as_object() { @@ -457,7 +496,7 @@ pub fn native_crc32(_this: &JsValue, args: &[JsValue], context: &mut Context) -> /// Formats USDA point3f triplets directly from raw IEEE-754 float bytes without intermediate /// Vec allocation. -fn format_usda_points_from_bytes(bytes: &[u8]) -> String { +pub fn format_usda_points_from_bytes(bytes: &[u8]) -> String { let pos_count = bytes.len() / 12; let mut s = String::with_capacity(pos_count * 32); for i in 0..pos_count { @@ -491,6 +530,7 @@ fn format_usda_points_from_bytes(bytes: &[u8]) -> String { } /// Native Boa hook for formatting USDA points directly from JS TypedArray buffers. +#[cfg(feature = "boa")] pub fn native_format_usda_points( _this: &JsValue, args: &[JsValue], @@ -499,18 +539,25 @@ pub fn native_format_usda_points( if let Some(val) = args.first() { if let Some(obj) = val.as_object() { if let Ok(typed_arr) = JsTypedArray::from_object(obj.clone()) { - if let Ok(buf_val) = typed_arr.buffer(context) { - if let Some(buf_obj) = buf_val.as_object() { - if let Ok(arr_buf) = JsArrayBuffer::from_object(buf_obj.clone()) { - let offset = typed_arr.byte_offset(context).unwrap_or(0); - let len = typed_arr.byte_length(context).unwrap_or(0); - if let Some(data) = arr_buf.data() { - let end = offset.saturating_add(len).min(data.len()); - let start = offset.min(end); - let slice = &data[start..end]; - check_native_input_size(slice.len(), "native_format_usda_points")?; - let formatted = format_usda_points_from_bytes(slice); - return Ok(JsValue::from(js_string!(formatted))); + if typed_arr.kind() + == Some(boa_engine::builtins::typed_array::TypedArrayKind::Float32) + { + if let Ok(buf_val) = typed_arr.buffer(context) { + if let Some(buf_obj) = buf_val.as_object() { + if let Ok(arr_buf) = JsArrayBuffer::from_object(buf_obj.clone()) { + let offset = typed_arr.byte_offset(context).unwrap_or(0); + let len = typed_arr.byte_length(context).unwrap_or(0); + if let Some(data) = arr_buf.data() { + let end = offset.saturating_add(len).min(data.len()); + let start = offset.min(end); + let slice = &data[start..end]; + check_native_input_size( + slice.len(), + "native_format_usda_points", + )?; + let formatted = format_usda_points_from_bytes(slice); + return Ok(JsValue::from(js_string!(formatted))); + } } } } @@ -526,7 +573,7 @@ pub fn native_format_usda_points( } /// Formats USDA texCoord2f (u, 1 - v) pairs directly from raw IEEE-754 float bytes. -fn format_usda_uvs_from_bytes(bytes: &[u8]) -> String { +pub fn format_usda_uvs_from_bytes(bytes: &[u8]) -> String { let uv_count = bytes.len() / 8; let mut s = String::with_capacity(uv_count * 24); for i in 0..uv_count { @@ -552,6 +599,7 @@ fn format_usda_uvs_from_bytes(bytes: &[u8]) -> String { } /// Native Boa hook for formatting USDA UVs directly from JS Float32Array buffers. +#[cfg(feature = "boa")] pub fn native_format_usda_uvs( _this: &JsValue, args: &[JsValue], @@ -560,18 +608,22 @@ pub fn native_format_usda_uvs( if let Some(val) = args.first() { if let Some(obj) = val.as_object() { if let Ok(typed_arr) = JsTypedArray::from_object(obj.clone()) { - if let Ok(buf_val) = typed_arr.buffer(context) { - if let Some(buf_obj) = buf_val.as_object() { - if let Ok(arr_buf) = JsArrayBuffer::from_object(buf_obj.clone()) { - let offset = typed_arr.byte_offset(context).unwrap_or(0); - let len = typed_arr.byte_length(context).unwrap_or(0); - if let Some(data) = arr_buf.data() { - let end = offset.saturating_add(len).min(data.len()); - let start = offset.min(end); - let slice = &data[start..end]; - check_native_input_size(slice.len(), "native_format_usda_uvs")?; - let formatted = format_usda_uvs_from_bytes(slice); - return Ok(JsValue::from(js_string!(formatted))); + if typed_arr.kind() + == Some(boa_engine::builtins::typed_array::TypedArrayKind::Float32) + { + if let Ok(buf_val) = typed_arr.buffer(context) { + if let Some(buf_obj) = buf_val.as_object() { + if let Ok(arr_buf) = JsArrayBuffer::from_object(buf_obj.clone()) { + let offset = typed_arr.byte_offset(context).unwrap_or(0); + let len = typed_arr.byte_length(context).unwrap_or(0); + if let Some(data) = arr_buf.data() { + let end = offset.saturating_add(len).min(data.len()); + let start = offset.min(end); + let slice = &data[start..end]; + check_native_input_size(slice.len(), "native_format_usda_uvs")?; + let formatted = format_usda_uvs_from_bytes(slice); + return Ok(JsValue::from(js_string!(formatted))); + } } } } @@ -597,7 +649,35 @@ pub fn native_format_usda_uvs( Ok(JsValue::from(js_string!(""))) } +/// Formats USDA integer index arrays directly from raw `u16` (`is_u16 = true`) or `u32` bytes. +pub fn format_usda_ints_from_bytes(slice: &[u8], is_u16: bool) -> String { + if is_u16 { + let count = slice.len() / 2; + let mut s = String::with_capacity(count * 8); + for (i, c) in slice.as_chunks::<2>().0.iter().enumerate() { + let v = u16::from_ne_bytes([c[0], c[1]]); + s.push_str(&v.to_string()); + if i + 1 < count { + s.push_str(", "); + } + } + s + } else { + let count = slice.len() / 4; + let mut s = String::with_capacity(count * 8); + for (i, c) in slice.as_chunks::<4>().0.iter().enumerate() { + let v = u32::from_ne_bytes([c[0], c[1], c[2], c[3]]); + s.push_str(&v.to_string()); + if i + 1 < count { + s.push_str(", "); + } + } + s + } +} + /// Native Boa hook for formatting integer index arrays directly from JS Uint16Array/Uint32Array. +#[cfg(feature = "boa")] pub fn native_format_usda_ints( _this: &JsValue, args: &[JsValue], @@ -616,34 +696,12 @@ pub fn native_format_usda_ints( let start = offset.min(end); let slice = &data[start..end]; check_native_input_size(slice.len(), "native_format_usda_ints")?; - let kind = typed_arr.kind(); - if kind + let is_u16 = typed_arr.kind() == Some( boa_engine::builtins::typed_array::TypedArrayKind::Uint16, - ) - { - let count = slice.len() / 2; - let mut s = String::with_capacity(count * 8); - for (i, c) in slice.as_chunks::<2>().0.iter().enumerate() { - let v = u16::from_ne_bytes([c[0], c[1]]); - s.push_str(&v.to_string()); - if i + 1 < count { - s.push_str(", "); - } - } - return Ok(JsValue::from(js_string!(s))); - } else { - let count = slice.len() / 4; - let mut s = String::with_capacity(count * 8); - for (i, c) in slice.as_chunks::<4>().0.iter().enumerate() { - let v = u32::from_ne_bytes([c[0], c[1], c[2], c[3]]); - s.push_str(&v.to_string()); - if i + 1 < count { - s.push_str(", "); - } - } - return Ok(JsValue::from(js_string!(s))); - } + ); + let s = format_usda_ints_from_bytes(slice, is_u16); + return Ok(JsValue::from(js_string!(s))); } } } @@ -655,6 +713,7 @@ pub fn native_format_usda_ints( } /// Native Boa hook for assembling a 64-byte aligned USDZ ZIP package with zero entry copies. +#[cfg(feature = "boa")] pub fn native_build_usdz_zip( _this: &JsValue, args: &[JsValue], @@ -785,6 +844,7 @@ pub fn native_build_usdz_zip( /// Native Boa hook for UTF-8 encoding strings (`TextEncoder.prototype.encode`) in O(1) JS loop /// iterations so large USDA scene graphs (>250 KB) do not trip Boa's loop iteration limit. +#[cfg(feature = "boa")] pub fn native_utf8_encode( _this: &JsValue, args: &[JsValue], @@ -805,6 +865,7 @@ pub fn native_utf8_encode( /// Native Boa hook for UTF-8 decoding byte buffers (`TextDecoder.prototype.decode`) in O(1) JS /// loop iterations. +#[cfg(feature = "boa")] pub fn native_utf8_decode( _this: &JsValue, args: &[JsValue], @@ -832,7 +893,7 @@ fn srgb_byte_to_linear(b: u8) -> f64 { } } -pub(crate) fn encode_rgba_png_data_uri_impl( +pub fn encode_rgba_png_data_uri_impl( src: &[u8], src_w: usize, src_h: usize, @@ -1013,6 +1074,7 @@ pub(crate) fn encode_rgba_png_data_uri_impl( /// Native Boa hook for encoding RGBA8 pixel buffers (`THREE.CanvasTexture`) into stored-DEFLATE /// `data:image/png;base64,...` URIs + linear `avgColor` in O(1) JS loop iterations. +#[cfg(feature = "boa")] pub fn native_encode_rgba_png( _this: &JsValue, args: &[JsValue], @@ -1075,32 +1137,20 @@ pub fn native_encode_rgba_png( Ok(JsValue::from(arr)) } -/// Native Boa hook for decoding `data:image/(png|jpeg);base64,...` URIs during USDZ packaging -/// in O(1) JS loop iterations. -pub fn native_decode_b64_image( - _this: &JsValue, - args: &[JsValue], - context: &mut Context, -) -> JsResult { - let Some(val) = args.first() else { - return Ok(JsValue::null()); - }; - let Some(js_str) = val.as_string() else { - return Ok(JsValue::null()); - }; - check_native_input_size(js_str.len(), "native_decode_b64_image")?; - let s = js_str.to_std_string_escaped(); - check_native_input_size(s.len(), "native_decode_b64_image")?; - let Some(comma_idx) = s.find(',') else { - return Ok(JsValue::null()); - }; +/// Decodes `data:image/(png|jpeg|jpg);base64,...` URIs into raw PNG/JPEG bytes, recompressing +/// uncompressed stored PNGs via `miniz_oxide` for compact USDZ archives. +pub fn decode_b64_image_data_uri(s: &str) -> Option> { + if s.len() > SECURITY_3D_MAX_HEAP_BYTES { + return None; + } + let comma_idx = s.find(',')?; let header = s[..comma_idx].to_ascii_lowercase(); if !(header.starts_with("data:image/png") || header.starts_with("data:image/jpeg") || header.starts_with("data:image/jpg")) || !header.ends_with(";base64") { - return Ok(JsValue::null()); + return None; } let payload = s[comma_idx + 1..].as_bytes(); let mut out = Vec::with_capacity((payload.len() * 3) / 4); @@ -1120,14 +1170,14 @@ pub fn native_decode_b64_image( b'=' => { pad += 1; if pad > 2 || qlen < 2 { - return Ok(JsValue::null()); + return None; } 0 } - _ => return Ok(JsValue::null()), + _ => return None, }; if pad > 0 && b != b'=' { - return Ok(JsValue::null()); + return None; } quad[qlen] = v; qlen += 1; @@ -1146,25 +1196,128 @@ pub fn native_decode_b64_image( qlen = 0; if pad > 0 { if payload[byte_idx + 1..].iter().any(|c| !c.is_ascii_whitespace()) { - return Ok(JsValue::null()); + return None; } break; } } } - if qlen != 0 { - return Ok(JsValue::null()); + if qlen != 0 || out.len() > SECURITY_3D_MAX_HEAP_BYTES { + return None; } - check_native_input_size(out.len(), "native_decode_b64_image")?; - // USDZ packaging: DEFLATE the stored PNGs the in-engine encoders write. - if let Some(packed) = crate::render::recompress_stored_png(&out) { + if let Some(packed) = crate::kernels::recompress_stored_png(&out) { out = packed; } + Some(out) +} + +/// Native Boa hook for decoding `data:image/(png|jpeg);base64,...` URIs during USDZ packaging +/// in O(1) JS loop iterations. +#[cfg(feature = "boa")] +pub fn native_decode_b64_image( + _this: &JsValue, + args: &[JsValue], + context: &mut Context, +) -> JsResult { + let Some(val) = args.first() else { + return Ok(JsValue::null()); + }; + let Some(js_str) = val.as_string() else { + return Ok(JsValue::null()); + }; + check_native_input_size(js_str.len(), "native_decode_b64_image")?; + let s = js_str.to_std_string_escaped(); + check_native_input_size(s.len(), "native_decode_b64_image")?; + let Some(out) = decode_b64_image_data_uri(&s) else { + return Ok(JsValue::null()); + }; let arr = js_uint8_array_from_slice(&out, context)?; Ok(JsValue::from(arr)) } -/// Registers native USDZ accelerator functions on the Boa global object. +/// Native Boa hook for capturing compiled GLB bytes directly from `sceneToGLB()` when +/// `with_direct_glb_capture` is active. +#[cfg(feature = "boa")] +pub fn native_stash_glb( + _this: &JsValue, + args: &[JsValue], + context: &mut Context, +) -> JsResult { + if !CAPTURE_GLB_DIRECT.with(|c| c.get()) { + return Ok(JsValue::new(false)); + } + let Some(val) = args.first() else { + return Ok(JsValue::new(false)); + }; + if let Some(obj) = val.as_object() { + if let Ok(typed_arr) = JsTypedArray::from_object(obj.clone()) { + if let Ok(buf_val) = typed_arr.buffer(context) { + if let Some(buf_obj) = buf_val.as_object() { + if let Ok(arr_buf) = JsArrayBuffer::from_object(buf_obj) { + if let Some(data) = arr_buf.data() { + let offset = typed_arr.byte_offset(context).unwrap_or(0); + let len = typed_arr.byte_length(context).unwrap_or(0); + let end = offset.saturating_add(len).min(data.len()); + let start = offset.min(end); + let slice = &data[start..end]; + check_native_input_size(slice.len(), "native_stash_glb")?; + STASHED_GLB_BYTES.with(|s| *s.borrow_mut() = Some(slice.to_vec())); + return Ok(JsValue::new(true)); + } + } + } + } + } + } + let bytes = + extract_byte_buffer(val, context).map_err(|e| JsNativeError::typ().with_message(e))?; + STASHED_GLB_BYTES.with(|s| *s.borrow_mut() = Some(bytes)); + Ok(JsValue::new(true)) +} + +/// Native Boa hook for DEFLATE-recompressing stored `data:image/png;base64,...` URIs emitted by +/// procedural and AI texture encoders during `sceneToGLB`. +#[cfg(feature = "boa")] +pub fn native_compress_png_uri( + _this: &JsValue, + args: &[JsValue], + _context: &mut Context, +) -> JsResult { + let Some(val) = args.first() else { + return Ok(JsValue::null()); + }; + let Some(js_str) = val.as_string() else { + return Ok(JsValue::null()); + }; + check_native_input_size(js_str.len(), "native_compress_png_uri")?; + let s = js_str.to_std_string_escaped(); + check_native_input_size(s.len(), "native_compress_png_uri")?; + let compressed = crate::kernels::compress_png_data_uri(&s); + Ok(JsValue::from(js_string!(compressed.as_ref()))) +} + +/// Native Boa hook for decoding PNG bytes into `[width, height, rgba_uint8_array]`. +#[cfg(feature = "boa")] +pub fn native_decode_png( + _this: &JsValue, + args: &[JsValue], + context: &mut Context, +) -> JsResult { + let Some(val) = args.first() else { + return Ok(JsValue::null()); + }; + let bytes = + extract_byte_buffer(val, context).map_err(|e| JsNativeError::typ().with_message(e))?; + let Ok((w, h, rgba)) = crate::kernels::decode_png_rgba(&bytes) else { + return Ok(JsValue::null()); + }; + let arr = js_uint8_array_from_slice(&rgba, context)?; + let res = JsArray::from_iter([JsValue::new(w), JsValue::new(h), JsValue::from(arr)], context); + Ok(JsValue::from(res)) +} + +/// Registers native USDZ & GLB accelerator functions on the Boa global object. +#[cfg(feature = "boa")] pub fn register_native_usdz_callables(context: &mut Context) -> JsResult<()> { context.register_global_builtin_callable( js_string!("__three_live_native_crc32__"), @@ -1211,6 +1364,21 @@ pub fn register_native_usdz_callables(context: &mut Context) -> JsResult<()> { 1, NativeFunction::from_fn_ptr(native_decode_b64_image), )?; + context.register_global_builtin_callable( + js_string!("__three_live_native_stash_glb__"), + 1, + NativeFunction::from_fn_ptr(native_stash_glb), + )?; + context.register_global_builtin_callable( + js_string!("__three_live_native_compress_png_uri__"), + 1, + NativeFunction::from_fn_ptr(native_compress_png_uri), + )?; + context.register_global_builtin_callable( + js_string!("__three_live_native_decode_png__"), + 1, + NativeFunction::from_fn_ptr(native_decode_png), + )?; Ok(()) } @@ -1326,6 +1494,7 @@ mod tests { assert_eq!(entries_found, 2); } + #[cfg(feature = "boa")] #[test] fn test_direct_usdz_capture_and_from_slice() { let mut context = Context::default(); diff --git a/compiler/tests/kernels_embedder_test.rs b/compiler/tests/kernels_embedder_test.rs new file mode 100644 index 00000000..18bf3f94 --- /dev/null +++ b/compiler/tests/kernels_embedder_test.rs @@ -0,0 +1,762 @@ +// Copyright 2026 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! External Boa embedder & per-kernel conformance tests for `three_live_kernels` +//! (`THREE_LIVE_KERNELS_ABI_VERSION = 1`) and the standalone hosted compiler bundle +//! (`three-live-compiler-bundle.min.js` + `engine-requirements.json`). + +use std::ffi::CStr; +use std::rc::Rc; + +use boa_engine::{ + builtins::typed_array::TypedArrayKind, + context::HostHooks, + js_string, + module::IdleModuleLoader, + object::builtins::{ + AlignedVec, JsArray, JsArrayBuffer, JsFloat32Array, JsTypedArray, JsUint16Array, + JsUint32Array, JsUint8Array, + }, + vm::RuntimeLimits, + Context, JsNativeError, JsObject, JsResult, JsString, JsValue, NativeFunction, Script, Source, +}; +use three_live_compiler::kernels::{ + three_live_kernel_free, three_live_kernels_abi_version, three_live_kernels_table, + ThreeLiveKernelBuffer, ThreeLiveKernelInvokeFn, ThreeLiveKernelSlice, + THREE_LIVE_KERNELS_ABI_VERSION, THREE_LIVE_KERNEL_ERR_QUOTA_EXCEEDED, THREE_LIVE_KERNEL_OK, +}; + +const HOSTED_BUNDLE_JS: &str = three_live_compiler::engine::EMBEDDED_THREE_AUTHOR_ENGINE_JS; +const ENGINE_REQUIREMENTS_JSON: &str = include_str!("../../three-live/engine-requirements.json"); + +#[derive(Debug, Clone, Copy)] +struct EmbedderHostHooks { + max_buffer_bytes: u64, +} + +impl HostHooks for EmbedderHostHooks { + fn max_buffer_size(&self, _context: &mut Context) -> u64 { + self.max_buffer_bytes + } +} + +struct KernelBufGuard(ThreeLiveKernelBuffer); + +impl Drop for KernelBufGuard { + fn drop(&mut self) { + if !self.0.data.is_null() { + unsafe { + three_live_kernel_free(self.0.data, self.0.capacity); + } + self.0.data = std::ptr::null_mut(); + } + } +} + +fn make_u8_array(bytes: &[u8], ctx: &mut Context) -> JsResult { + let mut aligned = AlignedVec::with_capacity(64, bytes.len()); + aligned.extend_from_slice(bytes); + let buf = JsArrayBuffer::from_byte_block(aligned, ctx)?; + JsUint8Array::from_array_buffer(buf, ctx) +} + +fn make_f32_array_bytes(bytes: &[u8], ctx: &mut Context) -> JsResult { + let mut aligned = AlignedVec::with_capacity(64, bytes.len()); + aligned.extend_from_slice(bytes); + let buf = JsArrayBuffer::from_byte_block(aligned, ctx)?; + JsFloat32Array::from_array_buffer(buf, ctx) +} + +fn make_u16_array_bytes(bytes: &[u8], ctx: &mut Context) -> JsResult { + let mut aligned = AlignedVec::with_capacity(64, bytes.len()); + aligned.extend_from_slice(bytes); + let buf = JsArrayBuffer::from_byte_block(aligned, ctx)?; + JsUint16Array::from_array_buffer(buf, ctx) +} + +fn make_u32_array_bytes(bytes: &[u8], ctx: &mut Context) -> JsResult { + let mut aligned = AlignedVec::with_capacity(64, bytes.len()); + aligned.extend_from_slice(bytes); + let buf = JsArrayBuffer::from_byte_block(aligned, ctx)?; + JsUint32Array::from_array_buffer(buf, ctx) +} + +fn read_typed_bytes(val: &JsValue, ctx: &mut Context) -> (Vec, Option) { + let Some(obj) = val.as_object() else { + return (Vec::new(), None); + }; + if let Ok(ta) = JsTypedArray::from_object(obj.clone()) { + let kind = ta.kind(); + if let Ok(bv) = ta.buffer(ctx) { + if let Some(bo) = bv.as_object() { + if let Ok(ab) = JsArrayBuffer::from_object(bo) { + if let Some(data) = ab.data() { + let off = ta.byte_offset(ctx).unwrap_or(0); + let len = ta.byte_length(ctx).unwrap_or(0); + let end = off.saturating_add(len).min(data.len()); + let start = off.min(end); + return (data[start..end].to_vec(), kind); + } + } + } + } + } else if let Ok(ab) = JsArrayBuffer::from_object(obj) { + if let Some(data) = ab.data() { + return (data.to_vec(), None); + } + } + (Vec::new(), None) +} + +/// Generic C-ABI kernel binder: reads `three_live_kernels_table(&count)` and binds every entry on +/// `ctx` using ONLY `desc.js_global_name`, `desc.signature`, and `desc.invoke` — zero per-kernel +/// FFI glue or hardcoded kernel names. +fn bind_all_kernels_from_c_abi_table(ctx: &mut Context) -> usize { + assert_eq!(three_live_kernels_abi_version(), THREE_LIVE_KERNELS_ABI_VERSION); + let mut count = 0usize; + let table_ptr = unsafe { three_live_kernels_table(&mut count) }; + assert!(!table_ptr.is_null() && count >= 20); + let descriptors = unsafe { std::slice::from_raw_parts(table_ptr, count) }; + + for desc in descriptors { + let js_name = unsafe { CStr::from_ptr(desc.js_global_name).to_str().unwrap().to_string() }; + let sig = unsafe { CStr::from_ptr(desc.signature).to_str().unwrap().to_string() }; + let (in_part, out_part) = sig.split_once(")->").expect("valid signature"); + let in_tokens: Vec = in_part + .trim_start_matches('(') + .split(',') + .filter(|s| !s.is_empty()) + .map(|s| s.to_string()) + .collect(); + let ret_token = out_part.to_string(); + let invoke: ThreeLiveKernelInvokeFn = desc.invoke; + let arity = in_tokens.len(); + + let closure = unsafe { + NativeFunction::from_closure(move |_this, args, context| { + let mut scalars: Vec = Vec::new(); + let mut owned_slices: Vec> = Vec::new(); + + for (idx, tok) in in_tokens.iter().enumerate() { + let arg = args.get(idx).cloned().unwrap_or(JsValue::undefined()); + match tok.as_str() { + "f" => { + let n = if arg.is_undefined() { + f64::NAN + } else if let Some(b) = arg.as_boolean() { + if b { + 1.0 + } else { + 0.0 + } + } else { + arg.to_number(context)? + }; + scalars.push(n); + } + "b" => { + let (bytes, _) = read_typed_bytes(&arg, context); + owned_slices.push(bytes); + } + "u32s" => { + let (bytes, kind) = read_typed_bytes(&arg, context); + if kind == Some(TypedArrayKind::Uint16) { + let mut u32_bytes = Vec::with_capacity(bytes.len() * 2); + for c in bytes.as_chunks::<2>().0 { + let v = u16::from_ne_bytes([c[0], c[1]]) as u32; + u32_bytes.extend_from_slice(&v.to_ne_bytes()); + } + owned_slices.push(u32_bytes); + } else if kind == Some(TypedArrayKind::Uint32) { + owned_slices.push(bytes); + } else { + owned_slices.push(Vec::new()); + } + } + "i" => { + let (bytes, kind) = read_typed_bytes(&arg, context); + scalars.push(if kind == Some(TypedArrayKind::Uint16) { + 1.0 + } else { + 0.0 + }); + owned_slices.push(bytes); + } + "s" => { + if arg.is_undefined() || arg.is_null() { + owned_slices.push(Vec::new()); + } else { + let js_s = arg.to_string(context)?; + owned_slices.push(js_s.to_std_string_escaped().into_bytes()); + } + } + "a" => { + let mut f64_bytes = Vec::new(); + if let Some(obj) = arg.as_object() { + if let Ok(arr) = JsArray::from_object(obj) { + let len = arr.length(context)? as usize; + f64_bytes.reserve(len * 8); + for i in 0..len { + let v = + arr.at(i as i64, context)?.as_number().unwrap_or(0.0); + f64_bytes.extend_from_slice(&v.to_ne_bytes()); + } + } + } + owned_slices.push(f64_bytes); + } + "z" => { + let mut packed = Vec::new(); + if let Some(obj) = arg.as_object() { + let arr = JsArray::from_object(obj).map_err(|e| { + JsNativeError::typ().with_message(format!("{}", e)) + })?; + let len = arr.length(context)? as u32; + packed.extend_from_slice(&len.to_le_bytes()); + for i in 0..len { + let item = arr.at(i as i64, context)?; + let item_obj = item.as_object().ok_or_else(|| { + JsNativeError::typ().with_message("entry must be object") + })?; + let name = item_obj + .get(js_string!("filename"), context) + .ok() + .and_then(|v| { + v.as_string().map(|s| s.to_std_string_escaped()) + }) + .unwrap_or_else(|| format!("file_{}", i)); + let data_val = item_obj + .get(js_string!("data"), context) + .unwrap_or(JsValue::undefined()); + let (data, _) = read_typed_bytes(&data_val, context); + packed.extend_from_slice(&(name.len() as u32).to_le_bytes()); + packed.extend_from_slice(name.as_bytes()); + packed.extend_from_slice(&(data.len() as u32).to_le_bytes()); + packed.extend_from_slice(&data); + } + } + owned_slices.push(packed); + } + other => panic!("Unsupported input token {}", other), + } + } + + let c_slices: Vec = owned_slices + .iter() + .map(|v| ThreeLiveKernelSlice { data: v.as_ptr(), len: v.len() }) + .collect(); + + let guard = KernelBufGuard(invoke( + scalars.as_ptr(), + scalars.len(), + c_slices.as_ptr(), + c_slices.len(), + )); + + if guard.0.status != THREE_LIVE_KERNEL_OK { + let msg = format!("Kernel status error {}", guard.0.status); + return if guard.0.status == THREE_LIVE_KERNEL_ERR_QUOTA_EXCEEDED { + Err(JsNativeError::range().with_message(msg).into()) + } else { + Err(JsNativeError::typ().with_message(msg).into()) + }; + } + + let out_bytes: &[u8] = if guard.0.data.is_null() || guard.0.len == 0 { + &[] + } else { + std::slice::from_raw_parts(guard.0.data, guard.0.len) + }; + + match ret_token.as_str() { + "geom" => { + let p_len = + u32::from_ne_bytes(out_bytes[0..4].try_into().unwrap()) as usize * 4; + let n_len = + u32::from_ne_bytes(out_bytes[4..8].try_into().unwrap()) as usize * 4; + let u_len = + u32::from_ne_bytes(out_bytes[8..12].try_into().unwrap()) as usize * 4; + let i_len = + u32::from_ne_bytes(out_bytes[12..16].try_into().unwrap()) as usize * 4; + let mut o = 16usize; + let pos = make_f32_array_bytes(&out_bytes[o..o + p_len], context)?; + o += p_len; + let norm = make_f32_array_bytes(&out_bytes[o..o + n_len], context)?; + o += n_len; + let uvs = make_f32_array_bytes(&out_bytes[o..o + u_len], context)?; + o += u_len; + let idx = make_u32_array_bytes(&out_bytes[o..o + i_len], context)?; + let obj = JsObject::with_object_proto(context.intrinsics()); + obj.set(js_string!("positions"), pos, false, context)?; + obj.set(js_string!("normals"), norm, false, context)?; + obj.set(js_string!("uvs"), uvs, false, context)?; + obj.set(js_string!("indices"), idx, false, context)?; + Ok(JsValue::from(obj)) + } + "f32" => Ok(JsValue::from(make_f32_array_bytes(out_bytes, context)?)), + "idx" => { + if out_bytes.len() < 4 { + return Ok(JsValue::from(make_u16_array_bytes(&[], context)?)); + } + if out_bytes[0] != 0 { + Ok(JsValue::from(make_u32_array_bytes(&out_bytes[4..], context)?)) + } else { + Ok(JsValue::from(make_u16_array_bytes(&out_bytes[4..], context)?)) + } + } + "u8" => Ok(JsValue::from(make_u8_array(out_bytes, context)?)), + "u8?" => { + if guard.0.data.is_null() || out_bytes.is_empty() { + Ok(JsValue::null()) + } else { + Ok(JsValue::from(make_u8_array(&out_bytes[1..], context)?)) + } + } + "str" => { + let s = String::from_utf8_lossy(out_bytes).into_owned(); + Ok(JsValue::from(js_string!(s))) + } + "u32" => { + let v = u32::from_ne_bytes(out_bytes[0..4].try_into().unwrap()); + Ok(JsValue::new(v)) + } + "maps3?" => { + if guard.0.data.is_null() || out_bytes.len() < 4 { + return Ok(JsValue::null()); + } + let m = u32::from_ne_bytes(out_bytes[0..4].try_into().unwrap()) as usize; + let a = make_u8_array(&out_bytes[4..4 + m], context)?; + let n = make_u8_array(&out_bytes[4 + m..4 + 2 * m], context)?; + let orm = make_u8_array(&out_bytes[4 + 2 * m..4 + 3 * m], context)?; + Ok(JsArray::from_iter([a.into(), n.into(), orm.into()], context).into()) + } + "maps2?" => { + if guard.0.data.is_null() || out_bytes.len() < 4 { + return Ok(JsValue::null()); + } + let m = u32::from_ne_bytes(out_bytes[0..4].try_into().unwrap()) as usize; + let n = make_u8_array(&out_bytes[4..4 + m], context)?; + let orm = make_u8_array(&out_bytes[4 + m..4 + 2 * m], context)?; + Ok(JsArray::from_iter([n.into(), orm.into()], context).into()) + } + "pnguri?" => { + if guard.0.data.is_null() || out_bytes.len() < 32 { + return Ok(JsValue::null()); + } + let r = f64::from_ne_bytes(out_bytes[0..8].try_into().unwrap()); + let g = f64::from_ne_bytes(out_bytes[8..16].try_into().unwrap()); + let b = f64::from_ne_bytes(out_bytes[16..24].try_into().unwrap()); + let a = f64::from_ne_bytes(out_bytes[24..32].try_into().unwrap()); + let uri = String::from_utf8_lossy(&out_bytes[32..]).into_owned(); + Ok(JsArray::from_iter( + [ + JsValue::from(js_string!(uri)), + JsValue::new(r), + JsValue::new(g), + JsValue::new(b), + JsValue::new(a), + ], + context, + ) + .into()) + } + "pngrgba?" => { + if guard.0.data.is_null() || out_bytes.len() < 8 { + return Ok(JsValue::null()); + } + let w = u32::from_ne_bytes(out_bytes[0..4].try_into().unwrap()); + let h = u32::from_ne_bytes(out_bytes[4..8].try_into().unwrap()); + let rgba = make_u8_array(&out_bytes[8..], context)?; + Ok(JsArray::from_iter( + [JsValue::new(w), JsValue::new(h), rgba.into()], + context, + ) + .into()) + } + other => panic!("Unsupported return token {}", other), + } + }) + }; + + ctx.register_global_builtin_callable(JsString::from(js_name), arity, closure) + .expect("register kernel"); + } + count +} + +fn eval_budgeted(ctx: &mut Context, code: &str, opcodes: u32) -> Result { + let parsed = Script::parse(Source::from_bytes(code.as_bytes()), None, ctx) + .map_err(|e| format!("{}", e))?; + let mut fut = std::pin::pin!(parsed.evaluate_async_with_budget(ctx, opcodes)); + let waker = std::task::Waker::noop(); + let mut task_cx = std::task::Context::from_waker(waker); + let mut yields = 0u32; + loop { + match std::future::Future::poll(fut.as_mut(), &mut task_cx) { + std::task::Poll::Ready(Ok(v)) => return Ok(v), + std::task::Poll::Ready(Err(e)) => return Err(format!("{}", e)), + std::task::Poll::Pending => { + yields += 1; + if yields > 2000 { + return Err("Budgeted evaluation exceeded max yield count".to_string()); + } + } + } + } +} + +fn build_external_boa_host(bind_native_kernels: bool) -> Context { + let req: serde_json::Value = + serde_json::from_str(ENGINE_REQUIREMENTS_JSON).expect("engine-requirements.json"); + let max_buf = req["quotas"]["maxArrayBufferBytes"].as_u64().unwrap(); + let mut ctx = Context::builder() + .host_hooks(Rc::new(EmbedderHostHooks { max_buffer_bytes: max_buf })) + .module_loader(Rc::new(IdleModuleLoader)) + .build() + .expect("build Boa context"); + + let mut limits = RuntimeLimits::default(); + let loop_lim = req["runtimeLimits"]["loopIterationLimit"].as_u64().unwrap(); + limits.set_loop_iteration_limit(if bind_native_kernels { loop_lim } else { loop_lim * 20 }); + limits.set_recursion_limit(req["runtimeLimits"]["recursionLimit"].as_u64().unwrap() as usize); + limits.set_stack_size_limit(req["runtimeLimits"]["stackSizeLimit"].as_u64().unwrap() as usize); + ctx.set_runtime_limits(limits); + + if bind_native_kernels { + let bound = bind_all_kernels_from_c_abi_table(&mut ctx); + assert_eq!(bound, 24); + } + + ctx.eval(Source::from_bytes(HOSTED_BUNDLE_JS.as_bytes())) + .expect("evaluate standalone hosted compiler bundle"); + ctx +} + +#[test] +fn external_boa_embedder_compiles_glb_usdz_and_ai_ops_without_compiler_engine() { + let mut ctx = build_external_boa_host(true); + + // Install host contract + budgeted evalScript bridge + sandbox hardening. + ctx.eval(Source::from_bytes( + b"globalThis.__charged = 0;\ + globalThis.__three_live_host__ = {\ + version: 1,\ + now: () => 42.5,\ + log: () => {},\ + charge: (n) => { globalThis.__charged += n; }\ + };", + )) + .unwrap(); + + // Verify sandbox hardening with `disableSyncCompile: false` for controlled `aiCall` tests, + // then also test `disableSyncCompile: true` budgeted flow below. + ctx.eval(Source::from_bytes(b"ThreeEngine.installSandboxHardening();")).unwrap(); + // Host contract is captured privately and hidden from untrusted user scripts. + let host_hidden = ctx + .eval(Source::from_bytes(b"typeof globalThis.__three_live_host__ === 'undefined'")) + .unwrap() + .to_boolean(); + assert!(host_hidden); + + // 1. Rich scene with Mechanical kit, CSG, ProceduralTexture, InstancedMesh, variants, TSL + let script = r#" + const gearGeo = new THREE.Mechanical.SpurGearGeometry({ teeth: 14, module: 0.04, thickness: 0.05 }); + const tex = THREE.ProceduralTexture.brushedMetal({ size: 32 }); + const matA = new THREE.MeshStandardMaterial({ ...tex, metalness: 0.9, roughness: 0.25 }); + const matB = new THREE.MeshStandardMaterial({ color: 0xf59e0b, roughness: 0.4 }); + const gear = new THREE.Mesh(gearGeo, matA); + gear.name = 'MainGear'; + gear.userData.variants = { Steel: matA, Brass: matB }; + gear.userData.xr = { grab: '1-hand', physics: 'dynamic' }; + scene.add(gear); + + const blockA = new THREE.Mesh(new THREE.BoxGeometry(0.4, 0.4, 0.4), matB); + const blockB = new THREE.Mesh(new THREE.SphereGeometry(0.25, 12, 8), matB); + const carved = THREE.CSG.subtract(blockA, blockB); + carved.name = 'CarvedHousing'; + carved.position.set(0.8, 0.2, 0); + scene.add(carved); + + const pins = new THREE.InstancedMesh(new THREE.CylinderGeometry(0.02, 0.02, 0.1, 8), matA, 4); + pins.name = 'Pins'; + for (let i = 0; i < 4; i++) { + pins.setMatrixAt(i, new THREE.Matrix4().makeTranslation(i * 0.1, 0, -0.4)); + } + scene.add(pins); + + const tslMat = new THREE.MeshStandardNodeMaterial(); + tslMat.colorNode = THREE.TSL.mix(THREE.TSL.vec3(0.1, 0.5, 0.9), THREE.TSL.vec3(0.9, 0.3, 0.2), THREE.TSL.uv().x); + const orb = new THREE.Mesh(new THREE.TorusKnotGeometry(0.15, 0.04, 24, 6), tslMat); + orb.name = 'ShaderOrb'; + orb.position.set(-0.7, 0.3, 0); + scene.add(orb); + "#; + + // Budgeted host evaluation flow (`wrapIsolatedScript` + `evaluate_async_with_budget`) + ctx.eval(Source::from_bytes(b"ThreeEngine.resetScene();")).unwrap(); + ctx.global_object() + .set(js_string!("__host_input_script__"), js_string!(script), false, &mut ctx) + .unwrap(); + let wrapped = ctx + .eval(Source::from_bytes(b"ThreeEngine.wrapIsolatedScript(__host_input_script__);")) + .unwrap() + .as_string() + .unwrap() + .to_std_string_escaped(); + + eval_budgeted(&mut ctx, &wrapped, 10_000).expect("budgeted user script execution"); + + let glb_val = eval_budgeted(&mut ctx, "ThreeEngine.serializeCurrentSceneToGLB();", 10_000) + .expect("serialize GLB"); + let (glb_bytes, _) = read_typed_bytes(&glb_val, &mut ctx); + assert!(glb_bytes.len() > 1000); + assert_eq!(&glb_bytes[0..4], b"glTF"); + let parsed_sg = three_live_compiler::read_glb(&glb_bytes).expect("valid GLB"); + assert_eq!(parsed_sg.meshes.len(), 4); + assert!(parsed_sg.variants.is_some()); + + let usdz_val = eval_budgeted(&mut ctx, "ThreeEngine.serializeCurrentSceneToUSDZ();", 10_000) + .expect("serialize USDZ"); + let (usdz_bytes, _) = read_typed_bytes(&usdz_val, &mut ctx); + assert_eq!(&usdz_bytes[0..4], b"PK\x03\x04"); + // Verify Apple 64-byte payload alignment on first entry + let fn_len = u16::from_le_bytes([usdz_bytes[26], usdz_bytes[27]]) as usize; + let extra_len = u16::from_le_bytes([usdz_bytes[28], usdz_bytes[29]]) as usize; + assert_eq!((30 + fn_len + extra_len) % 64, 0); + + // 2. glbToUSDZ + let glb_u8 = make_u8_array(&glb_bytes, &mut ctx).unwrap(); + ctx.global_object().set(js_string!("__host_glb__"), glb_u8, false, &mut ctx).unwrap(); + let converted_val = + eval_budgeted(&mut ctx, "ThreeEngine.glbToUSDZ(__host_glb__);", 10_000).unwrap(); + let (converted_usdz, _) = read_typed_bytes(&converted_val, &mut ctx); + assert_eq!(&converted_usdz[0..4], b"PK\x03\x04"); + + // 3. aiCall protocol ops (validate, describe, select, voice_setup, texture_key) + let ai_validate_req = serde_json::json!({ + "op": "validate", + "code": script, + }) + .to_string(); + ctx.global_object() + .set(js_string!("__ai_req__"), js_string!(ai_validate_req), false, &mut ctx) + .unwrap(); + let ai_resp_str = ctx + .eval(Source::from_bytes(b"ThreeEngine.aiCall(__ai_req__);")) + .unwrap() + .as_string() + .unwrap() + .to_std_string_escaped(); + let ai_resp: serde_json::Value = serde_json::from_str(&ai_resp_str).unwrap(); + assert_eq!(ai_resp["ok"], serde_json::json!(true)); + assert_eq!(ai_resp["meshCount"], serde_json::json!(4)); + + let ai_select_req = serde_json::json!({ + "op": "select", + "source": script, + "query": "the carved housing on the right" + }) + .to_string(); + ctx.global_object() + .set(js_string!("__ai_req__"), js_string!(ai_select_req), false, &mut ctx) + .unwrap(); + let sel_str = ctx + .eval(Source::from_bytes(b"ThreeEngine.aiCall(__ai_req__);")) + .unwrap() + .as_string() + .unwrap() + .to_std_string_escaped(); + let sel_resp: serde_json::Value = serde_json::from_str(&sel_str).unwrap(); + assert_eq!(sel_resp["names"], serde_json::json!(["CarvedHousing"])); + + // 4. Sandbox hardening blocks eval, Function, constructor chain, and sync compile when disabled + assert!(eval_budgeted(&mut ctx, "eval('1 + 1')", 10_000).is_err()); + assert!(eval_budgeted(&mut ctx, "(function(){}).constructor('return 1')()", 10_000).is_err()); + ctx.eval(Source::from_bytes( + b"ThreeEngine.installSandboxHardening({ disableSyncCompile: true });", + )) + .unwrap(); + assert!(eval_budgeted(&mut ctx, "ThreeEngine.compileToGLB('1+1')", 10_000).is_err()); +} + +#[test] +fn native_kernels_match_pure_js_fallbacks() { + let mut ctx_native = build_external_boa_host(true); + let mut ctx_fallback = build_external_boa_host(false); + + // Confirm fallback context has zero `__three_live_native_*__` hooks installed + let has_native = ctx_fallback + .eval(Source::from_bytes(b"typeof globalThis.__three_live_native_box__ === 'function'")) + .unwrap() + .to_boolean(); + assert!(!has_native); + + let probe = r#" + (() => { + const box = new THREE.BoxGeometry(1.5, 2.0, 0.5); + const plane = new THREE.PlaneGeometry(2.0, 3.0, 4, 3); + const sphere = new THREE.SphereGeometry(1.2, 12, 8); + const cyl = new THREE.CylinderGeometry(0.4, 0.6, 1.5, 12); + const torus = new THREE.TorusGeometry(1.0, 0.25, 8, 16); + const knot = new THREE.TorusKnotGeometry(0.8, 0.2, 24, 6, 2, 3); + + const custom = new THREE.BufferGeometry(); + custom.setAttribute('position', new THREE.BufferAttribute([0,0,0, 1,0,0, 0,1,0, 0,0,1], 3)); + custom.setIndex([0, 1, 2, 0, 2, 3]); + custom.computeVertexNormals(); + + const fnv = (u8) => { + let h = 0x811c9dc5; + for (let i = 0; i < u8.length; i++) { + h ^= u8[i]; + h = Math.imul(h, 0x01000193) >>> 0; + } + return h >>> 0; + }; + const presetHashes = []; + for (const name of ['checkerboard', 'grid', 'wood', 'marble', 'brushedMetal', 'carbonFiber', 'concrete', 'brick', 'fabric', 'leather', 'rust']) { + const maps = THREE.ProceduralTexture[name]({ size: 16, seed: 7 }); + presetHashes.push([ + fnv(maps.map.image._data), + fnv(maps.normalMap.image._data), + fnv(maps.roughnessMap.image._data), + maps.map.image.toDataURL(), + ]); + } + + const csgA = new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1), new THREE.MeshStandardMaterial()); + const csgB = new THREE.Mesh(new THREE.BoxGeometry(0.6, 0.6, 1.4), new THREE.MeshStandardMaterial()); + const csgOut = THREE.CSG.subtract(csgA, csgB); + + const utf8 = Array.from(new TextEncoder().encode('Hello Three-Live 🌍 Ω')); + const decoded = new TextDecoder().decode(new Uint8Array(utf8)); + + ThreeEngine.resetScene(); + const mesh = new THREE.Mesh(box, new THREE.MeshStandardMaterial(THREE.ProceduralTexture.checkerboard({ size: 16 }))); + mesh.name = 'CheckerBox'; + scene.add(mesh); + const usdz = scene.toUSDZ(); + + return JSON.stringify({ + boxPos: Array.from(box.attributes.position.array), + boxIdx: Array.from(box.index.array), + planePos: Array.from(plane.attributes.position.array), + planeIdx: Array.from(plane.index.array), + spherePos: Array.from(sphere.attributes.position.array), + sphereIdx: Array.from(sphere.index.array), + cylPos: Array.from(cyl.attributes.position.array), + cylIdx: Array.from(cyl.index.array), + torusPos: Array.from(torus.attributes.position.array), + torusIdx: Array.from(torus.index.array), + knotPos: Array.from(knot.attributes.position.array), + knotIdx: Array.from(knot.index.array), + customNorm: Array.from(custom.attributes.normal.array), + csgVertCount: csgOut.geometry.attributes.position.count, + presetHashes, + utf8, + decoded, + usdzMagic: [usdz[0], usdz[1], usdz[2], usdz[3]], + usdzAligned: ((30 + (usdz[26] | (usdz[27] << 8)) + (usdz[28] | (usdz[29] << 8))) % 64) === 0 + }); + })() + "#; + + let res_nat_val = ctx_native.eval(Source::from_bytes(probe.as_bytes())).unwrap_or_else(|e| { + let msg = e + .as_opaque() + .and_then(|v| v.to_string(&mut ctx_native).ok()) + .map(|s| s.to_std_string_escaped()) + .unwrap_or_else(|| format!("{:?}", e)); + panic!("ctx_native failed: {}", msg); + }); + let res_nat_str = res_nat_val.as_string().unwrap().to_std_string_escaped(); + let res_fb_val = ctx_fallback.eval(Source::from_bytes(probe.as_bytes())).unwrap_or_else(|e| { + let msg = e + .as_opaque() + .and_then(|v| v.to_string(&mut ctx_fallback).ok()) + .map(|s| s.to_std_string_escaped()) + .unwrap_or_else(|| format!("{:?}", e)); + panic!("ctx_fallback failed: {}", msg); + }); + let res_fb_str = res_fb_val.as_string().unwrap().to_std_string_escaped(); + + let nat: serde_json::Value = serde_json::from_str(&res_nat_str).unwrap(); + let fb: serde_json::Value = serde_json::from_str(&res_fb_str).unwrap(); + + // Exact byte/integer parity + for exact_key in [ + "boxPos", + "boxIdx", + "planePos", + "planeIdx", + "sphereIdx", + "cylIdx", + "torusIdx", + "knotIdx", + "csgVertCount", + "presetHashes", + "utf8", + "decoded", + "usdzMagic", + "usdzAligned", + ] { + assert_eq!(nat[exact_key], fb[exact_key], "exact parity mismatch on {}", exact_key); + } + + // Float trig kernels (`f32` vs `f64` sin/cos rounded to f32) agree within 1e-4 + for float_key in ["spherePos", "cylPos", "torusPos", "customNorm"] { + let a = nat[float_key].as_array().unwrap(); + let b = fb[float_key].as_array().unwrap(); + assert_eq!(a.len(), b.len(), "length mismatch on {}", float_key); + for (idx, (va, vb)) in a.iter().zip(b.iter()).enumerate() { + let da = va.as_f64().unwrap(); + let db = vb.as_f64().unwrap(); + assert!( + (da - db).abs() < 1e-4, + "{}[{}] diverged: native={} vs fallback={}", + float_key, + idx, + da, + db + ); + } + } + + // TorusKnot uses Frenet framing in Rust vs shared `buildSweptTube` framing in JS: + // verify identical vertex buffer length and matching 3D bounding box. + let kp_nat = nat["knotPos"].as_array().unwrap(); + let kp_fb = fb["knotPos"].as_array().unwrap(); + assert_eq!(kp_nat.len(), kp_fb.len()); + for axis in 0..3 { + let max_nat = kp_nat + .iter() + .skip(axis) + .step_by(3) + .map(|v| v.as_f64().unwrap()) + .fold(f64::NEG_INFINITY, f64::max); + let max_fb = kp_fb + .iter() + .skip(axis) + .step_by(3) + .map(|v| v.as_f64().unwrap()) + .fold(f64::NEG_INFINITY, f64::max); + assert!( + (max_nat - max_fb).abs() < 0.05, + "knot axis {} max mismatch: {} vs {}", + axis, + max_nat, + max_fb + ); + } +} diff --git a/compiler/tests/three_compiler_test.rs b/compiler/tests/three_compiler_test.rs index 83285513..b84e75e9 100644 --- a/compiler/tests/three_compiler_test.rs +++ b/compiler/tests/three_compiler_test.rs @@ -1047,16 +1047,12 @@ fn test_float64_array_is_widened() { 3, "Float64Array must be widened element-wise, not reinterpreted as f32" ); - assert_eq!( - pos["min"].as_array().expect("min present"), - &vec![serde_json::json!(0.0), serde_json::json!(0.0), serde_json::json!(0.0)], - "min must reflect the authored f64 values" - ); - assert_eq!( - pos["max"].as_array().expect("max present"), - &vec![serde_json::json!(1.0), serde_json::json!(1.0), serde_json::json!(0.0)], - "max must reflect the authored f64 values" - ); + let min_vals: Vec = + pos["min"].as_array().expect("min present").iter().map(|v| v.as_f64().unwrap()).collect(); + let max_vals: Vec = + pos["max"].as_array().expect("max present").iter().map(|v| v.as_f64().unwrap()).collect(); + assert_eq!(min_vals, vec![0.0, 0.0, 0.0], "min must reflect the authored f64 values"); + assert_eq!(max_vals, vec![1.0, 1.0, 0.0], "max must reflect the authored f64 values"); // A non-float element type must be rejected outright rather than reinterpreted. let bad = r#" @@ -1649,7 +1645,13 @@ fn test_empty_mesh_does_not_shift_interactivity_node_indices() { let gltf = parse_gltf_json(&glb); let nodes = gltf["nodes"].as_array().unwrap(); let target_idx = nodes.iter().position(|n| n["name"] == "Target").expect("Target node"); - assert_eq!(nodes[target_idx]["translation"], serde_json::json!([2.0, 0.0, 0.0])); + let target_t: Vec = nodes[target_idx]["translation"] + .as_array() + .unwrap() + .iter() + .map(|v| v.as_f64().unwrap()) + .collect(); + assert_eq!(target_t, vec![2.0, 0.0, 0.0]); let graph = gltf["extensions"]["KHR_interactivity"].to_string(); let ptr = format!("/nodes/{}/extensions/KHR_node_visibility/visible", target_idx); assert_eq!( @@ -1829,7 +1831,9 @@ fn test_rotated_parent_world_transform_in_glb_and_usdz() { for (got, want) in rot.iter().zip([0.0, 0.0, h, h]) { assert!((got - want).abs() < 1e-6, "parent rotation {:?}", rot); } - assert_eq!(nodes[1]["translation"], serde_json::json!([1.0, 0.0, 0.0])); + let child_t: Vec = + nodes[1]["translation"].as_array().unwrap().iter().map(|v| v.as_f64().unwrap()).collect(); + assert_eq!(child_t, vec![1.0, 0.0, 0.0]); assert!(nodes[1].get("rotation").is_none(), "child keeps its local (identity) rotation"); let usdz = compile_three_to_usdz(script).expect("USDZ should compile"); @@ -1913,29 +1917,62 @@ fn test_node_quota_limit() { assert!(err.contains("maximum allowed nodes"), "unexpected error: {}", err); } -/// A hand-built SceneGraph cannot describe a cycle or reference a missing mesh. +/// Cyclic JS hierarchy links, non-finite TRS, and out-of-range glTF mesh indices are sanitized. #[test] fn test_glb_writer_rejects_invalid_hierarchy_links() { - use three_live_compiler::scene::{SceneGraph, SceneNode}; - let scene = SceneGraph { - nodes: vec![ - SceneNode { name: "Root".into(), parent: Some(1), mesh: Some(7), ..Default::default() }, - SceneNode { - name: "Child".into(), - parent: Some(0), - rotation: [f32::NAN, 0.0, 0.0, 1.0], - translation: [f32::INFINITY, 1.0, 0.0], - ..Default::default() - }, - ], - ..Default::default() - }; - let gltf = parse_gltf_json(&three_live_compiler::write_glb(&scene).expect("GLB")); - assert_eq!(gltf["scenes"][0]["nodes"], serde_json::json!([0]), "forward parent -> root"); - assert_eq!(gltf["nodes"][0]["children"], serde_json::json!([1])); - assert!(gltf["nodes"][0].get("mesh").is_none(), "out-of-range mesh dropped"); + let script = r#" + const root = new THREE.Group(); + root.name = 'Root'; + const child = new THREE.Group(); + child.name = 'Child'; + child.quaternion.set(NaN, 0, 0, 1); + child.position.set(Infinity, 1, 0); + root.add(child); + // Force a cycle in children array directly + child.children.push(root); + scene.add(root); + "#; + let glb = transcode_three_to_glb(script).expect("GLB"); + let gltf = parse_gltf_json(&glb); + assert_eq!(gltf["scenes"][0]["nodes"], serde_json::json!([0]), "root node preserved"); + assert_eq!(gltf["nodes"][0]["children"], serde_json::json!([1]), "forward edge preserved"); + assert!(gltf["nodes"][1].get("children").is_none(), "cycle back-edge dropped without empty []"); assert!(gltf["nodes"][1].get("rotation").is_none(), "non-finite rotation -> identity"); - assert_eq!(gltf["nodes"][1]["translation"], serde_json::json!([0.0, 1.0, 0.0])); + let sanitized_t: Vec = gltf["nodes"][1]["translation"] + .as_array() + .unwrap() + .iter() + .map(|v| v.as_f64().unwrap()) + .collect(); + assert_eq!(sanitized_t, vec![0.0, 1.0, 0.0]); + + // Verify read_glb also drops out-of-range mesh indices and cyclic parent links safely. + let crafted_json = serde_json::json!({ + "asset": { "version": "2.0" }, + "scene": 0, + "scenes": [{ "nodes": [0] }], + "nodes": [ + { "name": "Root", "mesh": 7, "children": [1] }, + { "name": "Child", "children": [0] } + ] + }); + let mut json_bytes = serde_json::to_vec(&crafted_json).unwrap(); + while !json_bytes.len().is_multiple_of(4) { + json_bytes.push(0x20); + } + let total_len = (20 + json_bytes.len()) as u32; + let mut crafted_glb = Vec::with_capacity(total_len as usize); + crafted_glb.extend_from_slice(&0x46546C67u32.to_le_bytes()); + crafted_glb.extend_from_slice(&2u32.to_le_bytes()); + crafted_glb.extend_from_slice(&total_len.to_le_bytes()); + crafted_glb.extend_from_slice(&(json_bytes.len() as u32).to_le_bytes()); + crafted_glb.extend_from_slice(&0x4E4F534Au32.to_le_bytes()); + crafted_glb.extend_from_slice(&json_bytes); + let parsed_sg = three_live_compiler::read_glb(&crafted_glb).expect("read_glb"); + assert_eq!(parsed_sg.nodes.len(), 2); + assert!(parsed_sg.nodes[0].mesh.is_none(), "out-of-range mesh dropped"); + assert_eq!(parsed_sg.nodes[0].parent, None); + assert_eq!(parsed_sg.nodes[1].parent, Some(0)); } #[test] diff --git a/docs/xr-live.md b/docs/xr-live.md index 7c5e6de2..c4ffdf01 100644 --- a/docs/xr-live.md +++ b/docs/xr-live.md @@ -14,6 +14,8 @@ See the License for the specific language governing permissions and limitations under the License. --> + + # `` spatial computing and plugins guide `` extends `` into a full spatial computing runtime for @@ -61,10 +63,10 @@ browser WebXR (`navigator.xr`) alone has three practical limitations: `` addresses both web and native apps from a single scene definition: -* **On the web (``):** Starts with small core bundles (`< 52 KB gz` - for `xr-live-viewer.min.js`, or `< 73 KB gz` for `xr-live.min.js` with live +* **On the web (``):** Starts with small core bundles (`< 62 KB gz` + for `xr-live-viewer.min.js`, or `< 98 KB gz` for `xr-live.min.js` with live Three.js authoring) and optional plugins for advanced features (`~2–5 KB gz` - each, or `< 61 KB gz` for all 15 combined). + each, or `< 65 KB gz` for all 15 combined). * **In native Android, iOS, and visionOS apps (`XRLiveRuntimeSession` and `XRLiveRealityKitSession`):** The in-process Rust compiler (`ThreeLiveCompiler`) compiles `