Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
bokuweb committed Sep 22, 2024
1 parent 94f7e6a commit e9ea0fe
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 12 deletions.
1 change: 1 addition & 0 deletions crates/reg_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ bytes = "1.7.1"
urlencoding = "2.1.3"
pathdiff = "0.2.1"
path-clean = "1.0.1"
thiserror = "1.0"

[dev-dependencies]
35 changes: 23 additions & 12 deletions crates/reg_core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod dir;
mod report;

use image_diff_rs::{DiffOption, DiffOutput};
use image_diff_rs::{DiffOption, DiffOutput, ImageDiffError};
use path_clean::PathClean;
use rayon::{prelude::*, ThreadPoolBuilder};
use report::Report;
Expand All @@ -10,6 +10,18 @@ use std::{
path::{Path, PathBuf},
};

use thiserror::Error;

#[derive(Error, Debug)]
pub enum CompareError {
#[error("file io error, {0}")]
File(#[from] io::Error),
#[error("image diff error, {0}")]
ImageDiff(#[from] ImageDiffError),
#[error("unknown error")]
Unknown,
}

static SUPPORTED_EXTENTIONS: [&str; 7] = ["tiff", "jpeg", "jpg", "gif", "png", "bmp", "webp"];

fn is_supported_extension(path: &Path) -> bool {
Expand Down Expand Up @@ -78,7 +90,7 @@ pub fn run(
expected_dir: impl AsRef<Path>,
diff_dir: impl AsRef<Path>,
options: Options,
) {
) -> Result<(), CompareError> {
let actual_dir = actual_dir.as_ref();
let expected_dir = expected_dir.as_ref();
let diff_dir = diff_dir.as_ref();
Expand All @@ -95,7 +107,7 @@ pub fn run(
.num_threads(options.concurrency.unwrap_or_else(|| 4))
.build()
.unwrap();
let result: Result<Vec<(PathBuf, DiffOutput)>, std::io::Error> = pool.install(|| {
let result: Result<Vec<(PathBuf, DiffOutput)>, CompareError> = pool.install(|| {
targets
.par_iter()
.map(|path| {
Expand All @@ -108,9 +120,7 @@ pub fn run(
threshold: Some(0.05),
include_anti_alias: Some(!options.enable_antialias.unwrap_or_default()),
},
);
// std::fs::write("./test.png", res.unwrap().diff_image)?;
let res = res.expect("TODO:");
)?;
Ok((path.clone(), res))
})
.inspect(|r| {
Expand All @@ -119,10 +129,9 @@ pub fn run(
}
})
.collect()
});

let result = result.expect("TODO:");
})?;

let result = result?;
let mut differences = BTreeSet::new();
let mut passed = BTreeSet::new();
let mut failed = BTreeSet::new();
Expand All @@ -142,7 +151,7 @@ pub fn run(
differences.insert(diff_image.clone());
// TODO:
diff_image.set_extension("webp");
std::fs::write(diff_dir.join(&diff_image), item.diff_image.clone()).expect("TODO:");
std::fs::write(diff_dir.join(&diff_image), item.diff_image.clone())?;
}
}

Expand All @@ -162,8 +171,10 @@ pub fn run(
});

if let Some(html) = report.html {
std::fs::write("./report.html", html).expect("TODO:");
}
std::fs::write("./report.html", html)?;
};
Ok(())

}

pub(crate) fn find_images(
Expand Down
91 changes: 91 additions & 0 deletions js/proxy.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// compiled from: https://github.com/toyobayashi/emnapi/blob/main/packages/wasi-threads/src/proxy.ts
// MIT License
//
// Copyright (c) 2021-present Toyobayashi
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
export const kIsProxy = Symbol('kIsProxy')

export const createInstanceProxy = (
instance: WebAssembly.Instance,
memory?: WebAssembly.Memory | (() => WebAssembly.Memory)
): WebAssembly.Instance => {
if ((instance as any)[kIsProxy]) return instance

// https://github.com/nodejs/help/issues/4102
const originalExports = instance.exports
const createHandler = function (target: WebAssembly.Exports): ProxyHandler<WebAssembly.Exports> {
const handlers = [
'apply',
'construct',
'defineProperty',
'deleteProperty',
'get',
'getOwnPropertyDescriptor',
'getPrototypeOf',
'has',
'isExtensible',
'ownKeys',
'preventExtensions',
'set',
'setPrototypeOf'
]
const handler: ProxyHandler<WebAssembly.Exports> = {}
for (let i = 0; i < handlers.length; i++) {
const name = handlers[i] as keyof ProxyHandler<WebAssembly.Exports>
handler[name] = function () {
const args = Array.prototype.slice.call(arguments, 1)
args.unshift(target)
return (Reflect[name] as any).apply(Reflect, args)
}
}
return handler
}
const handler = createHandler(originalExports)
const _initialize = (): void => {}
const _start = (): number => 0
handler.get = function (_target, p, receiver) {
if (p === 'memory') {
return (typeof memory === 'function' ? memory() : memory) ?? Reflect.get(originalExports, p, receiver)
}
if (p === '_initialize') {
return p in originalExports ? _initialize : undefined
}
if (p === '_start') {
return p in originalExports ? _start : undefined
}
return Reflect.get(originalExports, p, receiver)
}
handler.has = function (_target, p) {
if (p === 'memory') return true
return Reflect.has(originalExports, p)
}
const exportsProxy = new Proxy(Object.create(null), handler)
return new Proxy(instance, {
get (target, p, receiver) {
if (p === 'exports') {
return exportsProxy
}
if (p === kIsProxy) {
return true
}
return Reflect.get(target, p, receiver)
}
})
}

0 comments on commit e9ea0fe

Please sign in to comment.