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;
|
||||
|
||||
fn encode_string(str: String) -> String {
|
||||
encode(&str).to_string()
|
||||
encode(&str).to_string()
|
||||
}
|
||||
|
||||
fn dorewrite(data: &str) -> Result<RewriteResult> {
|
||||
rewrite(
|
||||
data,
|
||||
Config {
|
||||
prefix: "/scrammedjet/".to_string(),
|
||||
encoder: Box::new(encode_string),
|
||||
rewrite(
|
||||
data,
|
||||
Config {
|
||||
prefix: "/scrammedjet/".to_string(),
|
||||
encoder: Box::new(encode_string),
|
||||
|
||||
base: Url::from_str("https://google.com/glorngle/si.js").context("invalid base")?,
|
||||
sourcetag: "glongle1".to_string(),
|
||||
base: Url::from_str("https://google.com/glorngle/si.js").context("invalid base")?,
|
||||
sourcetag: "glongle1".to_string(),
|
||||
|
||||
wrapfn: "$wrap".to_string(),
|
||||
wrapthisfn: "$gwrap".to_string(),
|
||||
importfn: "$import".to_string(),
|
||||
rewritefn: "$rewrite".to_string(),
|
||||
metafn: "$meta".to_string(),
|
||||
setrealmfn: "$setrealm".to_string(),
|
||||
pushsourcemapfn: "$pushsourcemap".to_string(),
|
||||
wrapfn: "$wrap".to_string(),
|
||||
wrapthisfn: "$gwrap".to_string(),
|
||||
importfn: "$import".to_string(),
|
||||
rewritefn: "$rewrite".to_string(),
|
||||
metafn: "$meta".to_string(),
|
||||
setrealmfn: "$setrealm".to_string(),
|
||||
pushsourcemapfn: "$pushsourcemap".to_string(),
|
||||
|
||||
capture_errors: true,
|
||||
do_sourcemaps: true,
|
||||
scramitize: false,
|
||||
strict_rewrites: true,
|
||||
},
|
||||
)
|
||||
.context("failed to rewrite file")
|
||||
capture_errors: true,
|
||||
do_sourcemaps: true,
|
||||
scramitize: false,
|
||||
strict_rewrites: true,
|
||||
},
|
||||
)
|
||||
.context("failed to rewrite file")
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
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 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 bench = env::args().nth(2).is_some();
|
||||
|
||||
let res = dorewrite(&data)?;
|
||||
if bench {
|
||||
loop {
|
||||
dorewrite(&data)?;
|
||||
}
|
||||
}
|
||||
|
||||
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()));
|
||||
}
|
||||
let res = dorewrite(&data)?;
|
||||
|
||||
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!(
|
||||
"rewritten:\n{}",
|
||||
String::from_utf8(res.js).context("failed to parse rewritten js")?
|
||||
);
|
||||
println!("changes: {:#?}", res.changes);
|
||||
|
||||
Ok(())
|
||||
println!(
|
||||
"rewritten:\n{}",
|
||||
String::from_utf8(res.js).context("failed to parse rewritten js")?
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use std::fs;
|
||||
use std::fs;
|
||||
|
||||
use boa_engine::{
|
||||
js_str, js_string,
|
||||
object::ObjectInitializer,
|
||||
property::{Attribute, PropertyDescriptorBuilder},
|
||||
Context, NativeFunction, Source,
|
||||
};
|
||||
use boa_engine::{
|
||||
js_str, js_string,
|
||||
object::ObjectInitializer,
|
||||
property::{Attribute, PropertyDescriptorBuilder},
|
||||
Context, NativeFunction, Source,
|
||||
};
|
||||
|
||||
use crate::dorewrite;
|
||||
use crate::dorewrite;
|
||||
|
||||
#[test]
|
||||
fn google() {
|
||||
let source_text = include_str!("../sample/google.js");
|
||||
dorewrite(source_text).unwrap();
|
||||
}
|
||||
#[test]
|
||||
fn google() {
|
||||
let source_text = include_str!("../sample/google.js");
|
||||
dorewrite(source_text).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
let files = fs::read_dir("./tests").unwrap();
|
||||
#[test]
|
||||
fn test() {
|
||||
let files = fs::read_dir("./tests").unwrap();
|
||||
|
||||
for file in files {
|
||||
if !file
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.file_name()
|
||||
.to_str()
|
||||
.unwrap()
|
||||
.ends_with(".js")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
for file in files {
|
||||
if !file
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.file_name()
|
||||
.to_str()
|
||||
.unwrap()
|
||||
.ends_with(".js")
|
||||
{
|
||||
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();
|
||||
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();
|
||||
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
|
||||
.register_global_callable(
|
||||
js_string!("fail"),
|
||||
0,
|
||||
NativeFunction::from_copy_closure(|_, _, _| {
|
||||
panic!("fail");
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let result = context
|
||||
.eval(Source::from_bytes(
|
||||
br#"
|
||||
let result = context
|
||||
.eval(Source::from_bytes(
|
||||
br#"
|
||||
function $wrap(val) {
|
||||
if (val === window || val === "location" || val === globalThis) return "";
|
||||
|
||||
|
@ -153,14 +160,14 @@ function check(val) {
|
|||
if (val === window || val === "location") fail();
|
||||
}
|
||||
"#,
|
||||
))
|
||||
.unwrap();
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
let rewritten = dorewrite(&content).unwrap();
|
||||
println!("{:?}", rewritten);
|
||||
let rewritten = dorewrite(&content).unwrap();
|
||||
println!("{:?}", rewritten);
|
||||
|
||||
context.eval(Source::from_bytes(&rewritten.js)).unwrap();
|
||||
println!("PASS");
|
||||
}
|
||||
}
|
||||
context.eval(Source::from_bytes(&rewritten.js)).unwrap();
|
||||
println!("PASS");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -230,7 +230,6 @@ impl JsChanges {
|
|||
self.inner.sort();
|
||||
|
||||
for change in &self.inner {
|
||||
println!("{:?}", change);
|
||||
let span = change.get_span();
|
||||
let start = span.start as usize;
|
||||
let end = span.end as usize;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue