Skip to content

Commit 59f1505

Browse files
Merge pull request Chia-Network#47 from Chia-Network/20231002-uncurry-and-typescript-fixed
20231002 uncurry and typescript fixed
2 parents a4cc097 + 0fb1e15 commit 59f1505

File tree

5 files changed

+380
-284
lines changed

5 files changed

+380
-284
lines changed

wasm/src/api.rs

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ use wasm_bindgen::JsCast;
1212

1313
use clvmr::allocator::Allocator;
1414

15-
use clvm_tools_rs::classic::clvm::__type_compatibility__::{Bytes, Stream, UnvalidatedBytesFromType};
15+
use crate::jsval::{
16+
btreemap_to_object, get_property, js_object_from_sexp, js_pair, object_to_value,
17+
read_string_to_string_map, sexp_from_js_object,
18+
};
19+
use crate::objects::Program;
20+
use clvm_tools_rs::classic::clvm::__type_compatibility__::{
21+
Bytes, Stream, UnvalidatedBytesFromType,
22+
};
1623
use clvm_tools_rs::classic::clvm::serialize::sexp_to_stream;
1724
use clvm_tools_rs::classic::clvm_tools::clvmc::compile_clvm_inner;
1825
use clvm_tools_rs::classic::clvm_tools::stages::stage_0::DefaultProgramRunner;
@@ -30,11 +37,6 @@ use clvm_tools_rs::compiler::repl::Repl;
3037
use clvm_tools_rs::compiler::runtypes::RunFailure;
3138
use clvm_tools_rs::compiler::sexp::SExp;
3239
use clvm_tools_rs::compiler::srcloc::Srcloc;
33-
use crate::jsval::{
34-
btreemap_to_object, get_property, js_object_from_sexp, js_pair, object_to_value,
35-
read_string_to_string_map, sexp_from_js_object,
36-
};
37-
use crate::objects::Program;
3840

3941
#[cfg(feature = "wee_alloc")]
4042
#[global_allocator]
@@ -117,20 +119,14 @@ pub fn create_clvm_runner_err(error: String) -> JsValue {
117119

118120
fn create_clvm_runner_run_failure(err: &RunFailure) -> JsValue {
119121
match err {
120-
RunFailure::RunErr(l, e) => {
121-
create_clvm_runner_err(format!("{}: Error {}", l, e))
122-
}
123-
RunFailure::RunExn(l, e) => {
124-
create_clvm_runner_err(format!("{}: Exn {}", l, e))
125-
}
122+
RunFailure::RunErr(l, e) => create_clvm_runner_err(format!("{}: Error {}", l, e)),
123+
RunFailure::RunExn(l, e) => create_clvm_runner_err(format!("{}: Exn {}", l, e)),
126124
}
127125
}
128126

129127
fn create_clvm_compile_failure(err: &CompileErr) -> JsValue {
130128
match err {
131-
CompileErr(l, e) => {
132-
create_clvm_runner_err(format!("{}: Error {}", l, e))
133-
}
129+
CompileErr(l, e) => create_clvm_runner_err(format!("{}: Error {}", l, e)),
134130
}
135131
}
136132

@@ -144,7 +140,12 @@ impl CldbSingleBespokeOverride for JsBespokeOverride {
144140
// When the user returns, try to convert the result back to sexp.
145141
fn get_override(&self, env: Rc<SExp>) -> Result<Rc<SExp>, RunFailure> {
146142
let args = js_sys::Array::new();
147-
args.set(0, js_object_from_sexp(env.clone()).map_err(|_| RunFailure::RunErr(env.loc(), "error converting override value".to_string()))?);
143+
args.set(
144+
0,
145+
js_object_from_sexp(env.clone()).map_err(|_| {
146+
RunFailure::RunErr(env.loc(), "error converting override value".to_string())
147+
})?,
148+
);
148149
self.fun
149150
.apply(&JsValue::null(), &args)
150151
.map_err(|e| {
@@ -247,7 +248,9 @@ pub fn create_clvm_runner(
247248
#[wasm_bindgen]
248249
pub fn final_value(runner: i32) -> JsValue {
249250
with_runner(runner, |r| {
250-
r.cldbrun.final_result().map(|v| js_object_from_sexp(v).unwrap_or_else(|e| e))
251+
r.cldbrun
252+
.final_result()
253+
.map(|v| js_object_from_sexp(v).unwrap_or_else(|e| e))
251254
})
252255
.unwrap_or_else(JsValue::null)
253256
}
@@ -280,7 +283,10 @@ fn make_compile_output(result_stream: &Stream, symbol_table: &HashMap<String, St
280283
);
281284
let symbol_array = js_sys::Array::new();
282285
for (idx, (k, v)) in symbol_table.iter().enumerate() {
283-
symbol_array.set(idx as u32, js_pair(JsValue::from_str(k), JsValue::from_str(v)));
286+
symbol_array.set(
287+
idx as u32,
288+
js_pair(JsValue::from_str(k), JsValue::from_str(v)),
289+
);
284290
}
285291
let symbol_object = object_to_value(&js_sys::Object::from_entries(&symbol_array).unwrap());
286292
array.set(1, js_pair(JsValue::from_str("symbols"), symbol_object));
@@ -303,8 +309,7 @@ pub fn compile(input_js: JsValue, filename_js: JsValue, search_paths_js: Vec<JsV
303309
.map(|j| j.as_string().unwrap())
304310
.collect();
305311

306-
let opts = Rc::new(DefaultCompilerOpts::new(&filename))
307-
.set_search_paths(&search_paths);
312+
let opts = Rc::new(DefaultCompilerOpts::new(&filename)).set_search_paths(&search_paths);
308313
match compile_clvm_inner(
309314
&mut allocator,
310315
opts,
@@ -370,15 +375,13 @@ pub fn compose_run_function(
370375
));
371376
}
372377
};
373-
let hash_bytes = match Bytes::new_validated(Some(UnvalidatedBytesFromType::Hex(function_hash.clone()))) {
374-
Err(e) => {
375-
return create_clvm_compile_failure(&CompileErr(
376-
program.loc(),
377-
e.to_string(),
378-
));
379-
},
380-
Ok(x) => x,
381-
};
378+
let hash_bytes =
379+
match Bytes::new_validated(Some(UnvalidatedBytesFromType::Hex(function_hash.clone()))) {
380+
Err(e) => {
381+
return create_clvm_compile_failure(&CompileErr(program.loc(), e.to_string()));
382+
}
383+
Ok(x) => x,
384+
};
382385

383386
let function_path = match path_to_function(main_env.1.clone(), &hash_bytes.data().clone()) {
384387
Some(p) => p,
@@ -489,11 +492,14 @@ pub fn sexp_to_string(v: &JsValue) -> JsValue {
489492

490493
#[wasm_bindgen]
491494
pub fn h(v: String) -> Result<Vec<u8>, JsValue> {
492-
let hex_data = Bytes::new_validated(Some(UnvalidatedBytesFromType::Hex(v))).map_err(|_| js_sys::JsString::from("bad hex input"))?;
495+
let hex_data = Bytes::new_validated(Some(UnvalidatedBytesFromType::Hex(v)))
496+
.map_err(|_| js_sys::JsString::from("bad hex input"))?;
493497
Ok(hex_data.data().clone())
494498
}
495499

496500
#[wasm_bindgen]
497501
pub fn t(a: &JsValue, b: &JsValue) -> Result<JsValue, JsValue> {
498-
Program::as_pair_internal(&Program::cons_internal(&Program::to(a)?, &Program::to(b)?)?)
502+
Program::as_pair_internal(
503+
&Program::cons_internal(&Program::to_internal(a)?, &Program::to_internal(b)?)?.into(),
504+
)
499505
}

wasm/src/jsval.rs

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use js_sys;
21
use js_sys::JSON::stringify;
32
use js_sys::{Array, BigInt, Object, Reflect};
43
use wasm_bindgen::JsCast;
@@ -42,21 +41,27 @@ pub fn js_object_from_sexp(v: Rc<SExp>) -> Result<JsValue, JsValue> {
4241
match v.borrow() {
4342
SExp::Nil(_) => Ok(JsValue::null()),
4443
SExp::Integer(_, i) => Ok(JsValue::bigint_from_str(&i.to_string())),
45-
SExp::QuotedString(_, _, q) => {
46-
Ok(JsValue::from_str(&Bytes::new(Some(BytesFromType::Raw(q.clone()))).decode()))
47-
}
48-
SExp::Atom(_, q) => {
49-
Ok(JsValue::from_str(&Bytes::new(Some(BytesFromType::Raw(q.clone()))).decode()))
50-
}
44+
SExp::QuotedString(_, _, q) => Ok(JsValue::from_str(
45+
&Bytes::new(Some(BytesFromType::Raw(q.clone()))).decode(),
46+
)),
47+
SExp::Atom(_, q) => Ok(JsValue::from_str(
48+
&Bytes::new(Some(BytesFromType::Raw(q.clone()))).decode(),
49+
)),
5150
SExp::Cons(_, a, b) => {
5251
if let Some(lst) = v.proper_list() {
5352
let array = Array::new();
54-
for i in 0..lst.len() {
55-
array.set(i as u32, js_object_from_sexp(Rc::new(lst[i].clone())).unwrap_or_else(|e| e));
53+
for (i, _) in lst.iter().enumerate() {
54+
array.set(
55+
i as u32,
56+
js_object_from_sexp(Rc::new(lst[i].clone())).unwrap_or_else(|e| e),
57+
);
5658
}
5759
Ok(array_to_value(array))
5860
} else {
59-
t(&js_object_from_sexp(a.clone())?, &js_object_from_sexp(b.clone())?)
61+
t(
62+
&js_object_from_sexp(a.clone())?,
63+
&js_object_from_sexp(b.clone())?,
64+
)
6065
}
6166
}
6267
}
@@ -103,33 +108,37 @@ fn location(o: &Object) -> Option<Srcloc> {
103108
line: l,
104109
col: c,
105110
until: get_property(o, "until")
106-
.and_then(|lo| Object::try_from(&lo).map(|o| o.clone()))
111+
.and_then(|lo| Object::try_from(&lo).cloned())
107112
.and_then(|lo| location_lc_pair(&lo))
108113
.map(|(ll, lc)| Until { line: ll, col: lc }),
109114
})
110115
}
111116

112117
pub fn detect_serializable(loc: &Srcloc, v: &JsValue) -> Option<Rc<SExp>> {
113118
let serialize_key = JsValue::from_str("serialize");
114-
js_sys::Reflect::get(v, &serialize_key).ok().and_then(|serialize| {
115-
Reflect::apply(serialize.unchecked_ref(), v, &js_sys::Array::new()).ok().and_then(|array| {
116-
Array::try_from(array).ok().and_then(|array| {
117-
let mut bytes_array: Vec<u8> = vec![];
118-
for item in array.iter() {
119-
if let Some(n) = item.as_f64() {
120-
if n < 0.0 || n > 255.0 {
121-
return None;
119+
js_sys::Reflect::get(v, &serialize_key)
120+
.ok()
121+
.and_then(|serialize| {
122+
Reflect::apply(serialize.unchecked_ref(), v, &js_sys::Array::new())
123+
.ok()
124+
.and_then(|array| {
125+
Array::try_from(array).ok().and_then(|array| {
126+
let mut bytes_array: Vec<u8> = vec![];
127+
for item in array.iter() {
128+
if let Some(n) = item.as_f64() {
129+
if !(0.0..=255.0).contains(&n) {
130+
return None;
131+
}
132+
bytes_array.push(n as u8);
133+
} else {
134+
return None;
135+
}
122136
}
123-
bytes_array.push(n as u8);
124-
} else {
125-
return None;
126-
}
127-
}
128137

129-
return Some(Rc::new(SExp::QuotedString(loc.clone(), b'x', bytes_array)));
130-
})
138+
Some(Rc::new(SExp::QuotedString(loc.clone(), b'x', bytes_array)))
139+
})
140+
})
131141
})
132-
})
133142
}
134143

135144
pub fn detect_convertible(v: &JsValue) -> Result<Rc<SExp>, JsValue> {
@@ -146,9 +155,9 @@ pub fn detect_convertible(v: &JsValue) -> Result<Rc<SExp>, JsValue> {
146155
pub fn sexp_from_js_object(sstart: Srcloc, v: &JsValue) -> Option<Rc<SExp>> {
147156
// Already converted value.
148157
if let Ok(res) = js_cache_value_from_js(v) {
149-
find_cached_sexp(res.entry, &res.content).map(|result| {
150-
result.modern.clone()
151-
}).ok()
158+
find_cached_sexp(res.entry, &res.content)
159+
.map(|result| result.modern.clone())
160+
.ok()
152161
} else if v.is_bigint() {
153162
BigInt::new(v)
154163
.ok()
@@ -157,30 +166,28 @@ pub fn sexp_from_js_object(sstart: Srcloc, v: &JsValue) -> Option<Rc<SExp>> {
157166
.and_then(|v| v.parse::<Number>().ok())
158167
.map(|x| Rc::new(SExp::Integer(sstart.clone(), x)))
159168
} else if let Some(fval) = v.as_f64() {
160-
(fval as i64).to_bigint()
169+
(fval as i64)
170+
.to_bigint()
161171
.map(|x| Rc::new(SExp::Integer(sstart.clone(), x)))
162172
} else if let Some(g1_bytes) = detect_serializable(&sstart, v) {
163173
Some(g1_bytes)
164-
} else if let Some(converted) = detect_convertible(v).ok() {
174+
} else if let Ok(converted) = detect_convertible(v) {
165175
Some(converted)
166176
} else if Array::is_array(v) {
167177
let a = Array::from(v);
168-
let mut result_value = Rc::new(SExp::Nil(Srcloc::start(&"*js*".to_string())));
178+
let mut result_value = Rc::new(SExp::Nil(Srcloc::start("*js*")));
169179
for i_rev in 0..a.length() {
170180
let i = a.length() - i_rev - 1;
171-
match sexp_from_js_object(sstart.clone(), &a.get(i)) {
172-
Some(nv) => {
173-
result_value = Rc::new(SExp::Cons(nv.loc(), nv, result_value));
174-
}
175-
_ => {}
181+
if let Some(nv) = sexp_from_js_object(sstart.clone(), &a.get(i)) {
182+
result_value = Rc::new(SExp::Cons(nv.loc(), nv, result_value));
176183
}
177184
}
178185
Some(result_value)
179186
} else {
180187
Object::try_from(v)
181188
.map(|o| {
182189
let loc = get_property(o, "location")
183-
.and_then(|o| Object::try_from(&o).map(|o| o.clone()))
190+
.and_then(|o| Object::try_from(&o).cloned())
184191
.and_then(|o| location(&o))
185192
.unwrap_or_else(|| sstart.clone());
186193
get_property(o, "pair")
@@ -211,21 +218,21 @@ pub fn btreemap_to_object<'a>(iter: impl Iterator<Item = (&'a String, &'a String
211218
let entries = Array::new();
212219
for pv in iter {
213220
let pair = Array::new();
214-
pair.set(0, JsValue::from_str(&pv.0));
215-
pair.set(1, JsValue::from_str(&pv.1));
221+
pair.set(0, JsValue::from_str(pv.0));
222+
pair.set(1, JsValue::from_str(pv.1));
216223
let pair_ref: &JsValue = pair.as_ref();
217224
entries.set(entries.length(), pair_ref.clone());
218225
}
219-
object_to_value(&Object::from_entries(&entries).as_ref().unwrap())
226+
object_to_value(Object::from_entries(&entries).as_ref().unwrap())
220227
}
221228

222229
pub fn read_string_to_string_map(
223230
symbols: &js_sys::Object,
224231
) -> Result<HashMap<String, String>, String> {
225232
let mut result = HashMap::new();
226-
for ent in js_sys::Object::keys(&symbols).values() {
233+
for ent in js_sys::Object::keys(symbols).values() {
227234
let key = ent.unwrap().as_string().unwrap();
228-
match get_property(&symbols, &key).unwrap().as_string() {
235+
match get_property(symbols, &key).unwrap().as_string() {
229236
Some(val) => {
230237
result.insert(key, val);
231238
}

0 commit comments

Comments
 (0)