File size: 2,425 Bytes
bee6636
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#[cfg(test)]
mod test {
	use std::fs;

	use crate::rewriter::NativeRewriter;
	use boa_engine::{
		Context, NativeFunction, Source, js_str, js_string,
		object::ObjectInitializer,
		property::{Attribute, PropertyDescriptorBuilder},
	};

	fn create_context() -> Context {
		let mut context = Context::default();

		let window = ObjectInitializer::new(&mut context).build();
		context
			.register_global_property(js_str!("window"), window, Attribute::READONLY)
			.unwrap();
		context
			.global_object()
			.define_property_or_throw(
				js_str!("location"),
				PropertyDescriptorBuilder::new()
					.get(
						NativeFunction::from_copy_closure(|_, _, _| Ok(js_str!("location").into()))
							.to_js_function(context.realm()),
					)
					.set(
						NativeFunction::from_copy_closure(|_, _, _| {
							panic!("fail: window.location got set")
						})
						.to_js_function(context.realm()),
					)
					.build(),
				&mut context,
			)
			.unwrap();

		context
			.register_global_callable(
				js_string!("fail"),
				0,
				NativeFunction::from_copy_closure(|_, _, _| {
					panic!("fail");
				}),
			)
			.unwrap();

		context
			.eval(Source::from_bytes(
				br#"

function $wrap(val) {

	if (val === window || val === "location" || val === globalThis) return "";



    return val;

}



const $gwrap = $wrap;



function $scramitize(val) { return val }



function assert(val) {

	if (!val) fail();

}



function check(val) {

    if (val === window || val === "location") fail();

}

			    "#,
			))
			.unwrap();

		context
	}

	#[test]
	fn google() {
		let source_text = include_str!("../sample/google.js");

		let rewriter = NativeRewriter::default();
		rewriter.rewrite_default(source_text).unwrap();
	}

	#[test]
	fn rewrite_tests() {
		let files = fs::read_dir("./tests").unwrap();

		let mut rewriter = NativeRewriter::default();

		for file in files.map(|x| x.unwrap()) {
			if !file.path().extension().unwrap().eq_ignore_ascii_case("js") {
				continue;
			}

			let content = fs::read_to_string(file.path()).unwrap();

			let rewritten = rewriter.rewrite_default(&content).unwrap();
			println!("{}", std::str::from_utf8(&rewritten.js).unwrap());

			let mut ctx = create_context();

			ctx.eval(Source::from_bytes(rewritten.js.as_slice()))
				.unwrap();

			println!("PASS");

			rewriter.reset();
		}
	}
}