refactoring done default

This commit is contained in:
freestrings
2019-05-26 23:30:01 +09:00
parent 9a35357ddb
commit d2a5d9092e
20 changed files with 1513 additions and 1172 deletions

View File

@ -12,7 +12,7 @@ use std::collections::HashMap;
use std::ops::Deref;
use std::result;
use std::result::Result;
use std::sync::Mutex;
use std::sync::{Mutex, Arc};
use cfg_if::cfg_if;
use jsonpath::filter::value_filter::JsonValueFilter;
@ -23,6 +23,8 @@ use serde_json::Value;
use wasm_bindgen::*;
use wasm_bindgen::prelude::*;
use web_sys::console;
use std::cell::RefCell;
use jsonpath::select::path_map::PathMap;
cfg_if! {
if #[cfg(feature = "wee_alloc")] {
@ -43,13 +45,14 @@ cfg_if! {
}
fn filter_ref_value(json: RefValueWrapper, node: Node) -> JsValue {
let mut jf = JsonValueFilter::new_from_value(json);
jf.visit(node);
let taken = &jf.take_value();
match JsValue::from_serde(taken.deref()) {
Ok(js_value) => js_value,
Err(e) => JsValue::from_str(&format!("Json deserialize error: {:?}", e))
}
// let mut jf = JsonValueFilter::new(json, Arc::new(RefCell::new(PathMap::new())));
// jf.visit(node);
// let taken = &jf.clone_value();
// match JsValue::from_serde(taken.deref()) {
// Ok(js_value) => js_value,
// Err(e) => JsValue::from_str(&format!("Json deserialize error: {:?}", e))
// }
JsValue::from_str("")
}
fn into_serde_json<D>(js_value: &JsValue) -> Result<D, String>
@ -77,48 +80,51 @@ fn into_ref_value(js_value: &JsValue, node: Node) -> JsValue {
}
fn get_ref_value(js_value: JsValue, node: Node) -> JsValue {
match js_value.as_f64() {
Some(val) => {
match CACHE_JSON.lock().unwrap().get(&(val as usize)) {
Some(json) => filter_ref_value(json.clone(), node),
_ => JsValue::from_str("Invalid pointer")
}
}
_ => into_ref_value(&js_value, node)
}
// match js_value.as_f64() {
// Some(val) => {
// match CACHE_JSON.lock().unwrap().get(&(val as usize)) {
// Some(json) => filter_ref_value(json.clone(), node),
// _ => JsValue::from_str("Invalid pointer")
// }
// }
// _ => into_ref_value(&js_value, node)
// }
JsValue::from_str("")
}
lazy_static! {
static ref CACHE_JSON: Mutex<HashMap<usize, RefValueWrapper>> = Mutex::new(HashMap::new());
static ref CACHE_JSON_IDX: Mutex<usize> = Mutex::new(0);
}
//lazy_static! {
// static ref CACHE_JSON: Mutex<HashMap<usize, RefValueWrapper>> = Mutex::new(HashMap::new());
// static ref CACHE_JSON_IDX: Mutex<usize> = Mutex::new(0);
//}
#[wasm_bindgen(js_name = allocJson)]
pub extern fn alloc_json(js_value: JsValue) -> usize {
let result: result::Result<RefValue, String> = into_serde_json(&js_value);
match result {
Ok(json) => {
let mut map = CACHE_JSON.lock().unwrap();
if map.len() >= std::u8::MAX as usize {
return 0;
}
let mut idx = CACHE_JSON_IDX.lock().unwrap();
*idx += 1;
map.insert(*idx, json.into());
*idx
}
Err(e) => {
console::error_1(&e.into());
0
}
}
// let result: result::Result<RefValue, String> = into_serde_json(&js_value);
// match result {
// Ok(json) => {
// let mut map = CACHE_JSON.lock().unwrap();
// if map.len() >= std::u8::MAX as usize {
// return 0;
// }
//
// let mut idx = CACHE_JSON_IDX.lock().unwrap();
// *idx += 1;
// map.insert(*idx, json.into());
// *idx
// }
// Err(e) => {
// console::error_1(&e.into());
// 0
// }
// }
0
}
#[wasm_bindgen(js_name = deallocJson)]
pub extern fn dealloc_json(ptr: usize) -> bool {
let mut map = CACHE_JSON.lock().unwrap();
map.remove(&ptr).is_some()
// let mut map = CACHE_JSON.lock().unwrap();
// map.remove(&ptr).is_some()
false
}
#[wasm_bindgen]
@ -139,32 +145,35 @@ pub fn compile(path: &str) -> JsValue {
#[wasm_bindgen]
pub fn selector(js_value: JsValue) -> JsValue {
let json = match js_value.as_f64() {
Some(val) => {
match CACHE_JSON.lock().unwrap().get(&(val as usize)) {
Some(json) => json.clone(),
_ => return JsValue::from_str("Invalid pointer")
}
}
_ => {
match into_serde_json::<RefValue>(&js_value) {
Ok(json) => json.into(),
Err(e) => return JsValue::from_str(e.as_str())
}
}
};
// let json = match js_value.as_f64() {
// Some(val) => {
// match CACHE_JSON.lock().unwrap().get(&(val as usize)) {
// Some(json) => json.clone(),
// _ => return JsValue::from_str("Invalid pointer")
// }
// }
// _ => {
// match into_serde_json::<RefValue>(&js_value) {
// Ok(json) => json.into(),
// Err(e) => return JsValue::from_str(e.as_str())
// }
// }
let cb = Closure::wrap(Box::new(move |path: String| {
let mut parser = Parser::new(path.as_str());
match parser.compile() {
Ok(node) => filter_ref_value(json.clone(), node),
Err(e) => return JsValue::from_str(e.as_str())
}
}) as Box<Fn(String) -> JsValue>);
// };
let ret = cb.as_ref().clone();
cb.forget();
ret
// let cb = Closure::wrap(Box::new(move |path: String| {
// let mut parser = Parser::new(path.as_str());
// match parser.compile() {
// Ok(node) => filter_ref_value(json.clone(), node),
// Err(e) => return JsValue::from_str(e.as_str())
// }
// }) as Box<Fn(String) -> JsValue>);
//
// let ret = cb.as_ref().clone();
// cb.forget();
// ret
JsValue::from_str("")
}
#[wasm_bindgen]