Skip to content

Commit 490c9e6

Browse files
authored
Merge pull request #75 from Shopify/ap.improve-len-error-case
Improve length get error cases
2 parents 9c1199c + a631026 commit 490c9e6

File tree

6 files changed

+61
-9
lines changed

6 files changed

+61
-9
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ shopify_function_wasm_api_core = { path = "../core", version = "0.1.0" }
1212
thiserror = "2.0"
1313

1414
[target.'cfg(not(target_family = "wasm"))'.dependencies]
15-
shopify_function_provider = { path = "../provider", version = "1.0.0" }
15+
shopify_function_provider = { path = "../provider", version = "1.0.1" }
1616
serde_json = "1.0"
1717
rmp-serde = "1.3"
1818

api/src/lib.rs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,11 @@ impl Value {
409409
} else {
410410
len
411411
};
412-
Some(len)
412+
if len == usize::MAX {
413+
None
414+
} else {
415+
Some(len)
416+
}
413417
}
414418
_ => None,
415419
}
@@ -429,7 +433,11 @@ impl Value {
429433
} else {
430434
len
431435
};
432-
Some(len)
436+
if len == usize::MAX {
437+
None
438+
} else {
439+
Some(len)
440+
}
433441
}
434442
_ => None,
435443
}
@@ -569,4 +577,48 @@ mod tests {
569577
let id3 = cached_interned_string_id.load_from_context(&context2);
570578
assert_ne!(id, id3);
571579
}
580+
581+
#[test]
582+
fn test_array_len_with_null_ptr() {
583+
let context = Context::new_with_input(serde_json::json!({}));
584+
let value = Value {
585+
context: NonNull::new(context.0 as _).unwrap(),
586+
nan_box: NanBox::array(0, NanBox::MAX_VALUE_LENGTH),
587+
};
588+
let len = value.array_len();
589+
assert_eq!(len, None);
590+
}
591+
592+
#[test]
593+
fn test_array_len_with_non_length_eligible_nan_box() {
594+
let context = Context::new_with_input(serde_json::json!({}));
595+
let value = Value {
596+
context: NonNull::new(context.0 as _).unwrap(),
597+
nan_box: NanBox::null(),
598+
};
599+
let len = value.array_len();
600+
assert_eq!(len, None);
601+
}
602+
603+
#[test]
604+
fn test_obj_len_with_null_ptr() {
605+
let context = Context::new_with_input(serde_json::json!({}));
606+
let value = Value {
607+
context: NonNull::new(context.0 as _).unwrap(),
608+
nan_box: NanBox::obj(0, NanBox::MAX_VALUE_LENGTH),
609+
};
610+
let len = value.obj_len();
611+
assert_eq!(len, None);
612+
}
613+
614+
#[test]
615+
fn test_obj_len_with_non_length_eligible_nan_box() {
616+
let context = Context::new_with_input(serde_json::json!({}));
617+
let value = Value {
618+
context: NonNull::new(context.0 as _).unwrap(),
619+
nan_box: NanBox::null(),
620+
};
621+
let len = value.obj_len();
622+
assert_eq!(len, None);
623+
}
572624
}

api/src/shopify_function.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
;; - Array: number of elements.
4343
;; - String: number of utf8-encoded bytes.
4444
;; - Object: number of entries.
45-
;; - 0 for all other values.
45+
;; - -1 for all other values.
4646
;;
4747
;; Note that calling this function is not required in all cases, if
4848
;; the length value is equal or less than (2^14) - 1, the length

provider/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "shopify_function_provider"
3-
version = "1.0.0"
3+
version = "1.0.1"
44
edition = "2021"
55
license = "MIT"
66
repository = "https://github.com/Shopify/shopify-function-wasm-api"

provider/src/read.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,14 @@ decorate_for_target! {
172172
match v.try_decode() {
173173
Ok(NanBoxValueRef::String { ptr, .. } | NanBoxValueRef::Array { ptr, .. } | NanBoxValueRef::Object { ptr, .. }) => {
174174
let Ok(value) = LazyValueRef::mut_from_raw(ptr as _) else {
175-
return 0;
175+
return usize::MAX;
176176
};
177177
value.get_value_length()
178178
}
179-
_ => 0,
179+
_ => usize::MAX,
180180
}
181181
}
182-
Err(_) => 0,
182+
Err(_) => usize::MAX,
183183
}
184184
}
185185
}

0 commit comments

Comments
 (0)