mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-14 23:10:02 -04:00
fix
This commit is contained in:
parent
1549db48ad
commit
2f7d2fa043
3 changed files with 119 additions and 113 deletions
|
@ -7,138 +7,145 @@ use url::Url;
|
||||||
use urlencoding::encode;
|
use urlencoding::encode;
|
||||||
|
|
||||||
fn encode_string(str: String) -> String {
|
fn encode_string(str: String) -> String {
|
||||||
encode(&str).to_string()
|
encode(&str).to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dorewrite(data: &str) -> Result<RewriteResult> {
|
fn dorewrite(data: &str) -> Result<RewriteResult> {
|
||||||
rewrite(
|
rewrite(
|
||||||
data,
|
data,
|
||||||
Config {
|
Config {
|
||||||
prefix: "/scrammedjet/".to_string(),
|
prefix: "/scrammedjet/".to_string(),
|
||||||
encoder: Box::new(encode_string),
|
encoder: Box::new(encode_string),
|
||||||
|
|
||||||
base: Url::from_str("https://google.com/glorngle/si.js").context("invalid base")?,
|
base: Url::from_str("https://google.com/glorngle/si.js").context("invalid base")?,
|
||||||
sourcetag: "glongle1".to_string(),
|
sourcetag: "glongle1".to_string(),
|
||||||
|
|
||||||
wrapfn: "$wrap".to_string(),
|
wrapfn: "$wrap".to_string(),
|
||||||
wrapthisfn: "$gwrap".to_string(),
|
wrapthisfn: "$gwrap".to_string(),
|
||||||
importfn: "$import".to_string(),
|
importfn: "$import".to_string(),
|
||||||
rewritefn: "$rewrite".to_string(),
|
rewritefn: "$rewrite".to_string(),
|
||||||
metafn: "$meta".to_string(),
|
metafn: "$meta".to_string(),
|
||||||
setrealmfn: "$setrealm".to_string(),
|
setrealmfn: "$setrealm".to_string(),
|
||||||
pushsourcemapfn: "$pushsourcemap".to_string(),
|
pushsourcemapfn: "$pushsourcemap".to_string(),
|
||||||
|
|
||||||
capture_errors: true,
|
capture_errors: true,
|
||||||
do_sourcemaps: true,
|
do_sourcemaps: true,
|
||||||
scramitize: false,
|
scramitize: false,
|
||||||
strict_rewrites: true,
|
strict_rewrites: true,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.context("failed to rewrite file")
|
.context("failed to rewrite file")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let file = env::args().nth(1).unwrap_or_else(|| "test.js".to_string());
|
let file = env::args().nth(1).unwrap_or_else(|| "test.js".to_string());
|
||||||
let data = fs::read_to_string(file).context("failed to read file")?;
|
let data = fs::read_to_string(file).context("failed to read file")?;
|
||||||
|
let bench = env::args().nth(2).is_some();
|
||||||
|
|
||||||
let res = dorewrite(&data)?;
|
if bench {
|
||||||
|
loop {
|
||||||
|
dorewrite(&data)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let source = Arc::new(
|
let res = dorewrite(&data)?;
|
||||||
NamedSource::new(data, "https://google.com/glorngle/si.js").with_language("javascript"),
|
|
||||||
);
|
|
||||||
eprintln!("errors:");
|
|
||||||
for err in res.errors {
|
|
||||||
eprintln!("{}", err.with_source_code(source.clone()));
|
|
||||||
}
|
|
||||||
|
|
||||||
println!("changes: {:#?}", res.changes);
|
let source = Arc::new(
|
||||||
|
NamedSource::new(data, "https://google.com/glorngle/si.js").with_language("javascript"),
|
||||||
|
);
|
||||||
|
eprintln!("errors:");
|
||||||
|
for err in res.errors {
|
||||||
|
eprintln!("{}", err.with_source_code(source.clone()));
|
||||||
|
}
|
||||||
|
|
||||||
println!(
|
println!("changes: {:#?}", res.changes);
|
||||||
"rewritten:\n{}",
|
|
||||||
String::from_utf8(res.js).context("failed to parse rewritten js")?
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(())
|
println!(
|
||||||
|
"rewritten:\n{}",
|
||||||
|
String::from_utf8(res.js).context("failed to parse rewritten js")?
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
use boa_engine::{
|
use boa_engine::{
|
||||||
js_str, js_string,
|
js_str, js_string,
|
||||||
object::ObjectInitializer,
|
object::ObjectInitializer,
|
||||||
property::{Attribute, PropertyDescriptorBuilder},
|
property::{Attribute, PropertyDescriptorBuilder},
|
||||||
Context, NativeFunction, Source,
|
Context, NativeFunction, Source,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::dorewrite;
|
use crate::dorewrite;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn google() {
|
fn google() {
|
||||||
let source_text = include_str!("../sample/google.js");
|
let source_text = include_str!("../sample/google.js");
|
||||||
dorewrite(source_text).unwrap();
|
dorewrite(source_text).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test() {
|
fn test() {
|
||||||
let files = fs::read_dir("./tests").unwrap();
|
let files = fs::read_dir("./tests").unwrap();
|
||||||
|
|
||||||
for file in files {
|
for file in files {
|
||||||
if !file
|
if !file
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.file_name()
|
.file_name()
|
||||||
.to_str()
|
.to_str()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.ends_with(".js")
|
.ends_with(".js")
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let content = fs::read_to_string(file.unwrap().path()).unwrap();
|
let content = fs::read_to_string(file.unwrap().path()).unwrap();
|
||||||
|
|
||||||
let mut context = Context::default();
|
let mut context = Context::default();
|
||||||
|
|
||||||
let window = ObjectInitializer::new(&mut context).build();
|
let window = ObjectInitializer::new(&mut context).build();
|
||||||
context
|
context
|
||||||
.register_global_property(js_str!("window"), window, Attribute::READONLY)
|
.register_global_property(js_str!("window"), window, Attribute::READONLY)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
context
|
context
|
||||||
.global_object()
|
.global_object()
|
||||||
.define_property_or_throw(
|
.define_property_or_throw(
|
||||||
js_str!("location"),
|
js_str!("location"),
|
||||||
PropertyDescriptorBuilder::new()
|
PropertyDescriptorBuilder::new()
|
||||||
.get(
|
.get(
|
||||||
NativeFunction::from_copy_closure(|_, _, _| {
|
NativeFunction::from_copy_closure(|_, _, _| {
|
||||||
Ok(js_str!("location").into())
|
Ok(js_str!("location").into())
|
||||||
})
|
})
|
||||||
.to_js_function(context.realm()),
|
.to_js_function(context.realm()),
|
||||||
)
|
)
|
||||||
.set(
|
.set(
|
||||||
NativeFunction::from_copy_closure(|_, _, _| {
|
NativeFunction::from_copy_closure(|_, _, _| {
|
||||||
panic!("fail: window.location got set")
|
panic!("fail: window.location got set")
|
||||||
})
|
})
|
||||||
.to_js_function(context.realm()),
|
.to_js_function(context.realm()),
|
||||||
)
|
)
|
||||||
.build(),
|
.build(),
|
||||||
&mut context,
|
&mut context,
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
context
|
context
|
||||||
.register_global_callable(
|
.register_global_callable(
|
||||||
js_string!("fail"),
|
js_string!("fail"),
|
||||||
0,
|
0,
|
||||||
NativeFunction::from_copy_closure(|_, _, _| {
|
NativeFunction::from_copy_closure(|_, _, _| {
|
||||||
panic!("fail");
|
panic!("fail");
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let result = context
|
let result = context
|
||||||
.eval(Source::from_bytes(
|
.eval(Source::from_bytes(
|
||||||
br#"
|
br#"
|
||||||
function $wrap(val) {
|
function $wrap(val) {
|
||||||
if (val === window || val === "location" || val === globalThis) return "";
|
if (val === window || val === "location" || val === globalThis) return "";
|
||||||
|
|
||||||
|
@ -153,14 +160,14 @@ function check(val) {
|
||||||
if (val === window || val === "location") fail();
|
if (val === window || val === "location") fail();
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
))
|
))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let rewritten = dorewrite(&content).unwrap();
|
let rewritten = dorewrite(&content).unwrap();
|
||||||
println!("{:?}", rewritten);
|
println!("{:?}", rewritten);
|
||||||
|
|
||||||
context.eval(Source::from_bytes(&rewritten.js)).unwrap();
|
context.eval(Source::from_bytes(&rewritten.js)).unwrap();
|
||||||
println!("PASS");
|
println!("PASS");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -230,7 +230,6 @@ impl JsChanges {
|
||||||
self.inner.sort();
|
self.inner.sort();
|
||||||
|
|
||||||
for change in &self.inner {
|
for change in &self.inner {
|
||||||
println!("{:?}", change);
|
|
||||||
let span = change.get_span();
|
let span = change.get_span();
|
||||||
let start = span.start as usize;
|
let start = span.start as usize;
|
||||||
let end = span.end as usize;
|
let end = span.end as usize;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue