feat: 实现 URI 全局编码解码函数
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
use oxide_compiler::compiler::Compiler;
|
||||
use oxide_parser::Allocator;
|
||||
use oxide_types::value::JsValue;
|
||||
use oxide_vm::vm::Vm;
|
||||
|
||||
fn eval(source: &str) -> Result<oxide_types::value::JsValue, String> {
|
||||
fn eval(source: &str) -> Result<JsValue, String> {
|
||||
let allocator = Allocator::default();
|
||||
let program = oxide_parser::parse(&allocator, source).map_err(|e| format!("Parse error: {:?}", e))?;
|
||||
let module = Compiler::new().compile(&program).map_err(|e| format!("Compile error: {}", e))?;
|
||||
@@ -10,6 +11,22 @@ fn eval(source: &str) -> Result<oxide_types::value::JsValue, String> {
|
||||
vm.run(&module)
|
||||
}
|
||||
|
||||
fn eval_with_vm(source: &str) -> (Vm, Result<JsValue, String>) {
|
||||
let allocator = Allocator::default();
|
||||
let program = oxide_parser::parse(&allocator, source).expect("parse");
|
||||
let module = Compiler::new().compile(&program).expect("compile");
|
||||
let mut vm = Vm::new();
|
||||
let result = vm.run(&module);
|
||||
(vm, result)
|
||||
}
|
||||
|
||||
fn str_val(vm: &Vm, val: JsValue) -> String {
|
||||
vm.kernel_core()
|
||||
.string_forge()
|
||||
.lookup(val.as_string_index())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_nan_returns_nan() {
|
||||
let result = eval("NaN").unwrap();
|
||||
@@ -83,3 +100,34 @@ fn global_this_points_to_global_object() {
|
||||
assert!(result.is_bool());
|
||||
assert!(result.as_bool());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_uri_preserves_reserved_chars() {
|
||||
let (vm, result) = eval_with_vm("encodeURI('https://example.com/path?q=hello world')");
|
||||
assert_eq!(str_val(&vm, result.unwrap()), "https://example.com/path?q=hello%20world");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_uri_component_encodes_reserved_chars() {
|
||||
let (vm, result) = eval_with_vm("encodeURIComponent('a=1&b=2')");
|
||||
assert_eq!(str_val(&vm, result.unwrap()), "a%3D1%26b%3D2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_uri_preserves_escaped_reserved_chars() {
|
||||
let (vm, result) = eval_with_vm("decodeURI('https://example.com/path%3Fq=hello%20world')");
|
||||
assert_eq!(str_val(&vm, result.unwrap()), "https://example.com/path%3Fq=hello world");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_uri_component_decodes_reserved_chars() {
|
||||
let (vm, result) = eval_with_vm("decodeURIComponent('a%3D1%26b%3D2')");
|
||||
assert_eq!(str_val(&vm, result.unwrap()), "a=1&b=2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_uri_component_malformed_sequence_throws_uri_error() {
|
||||
let result = eval("try { decodeURIComponent('%') } catch (e) { e instanceof URIError }").unwrap();
|
||||
assert!(result.is_bool());
|
||||
assert!(result.as_bool());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user