Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
r58Playz committed Dec 11, 2024
1 parent 1549db4 commit 2f7d2fa
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 131 deletions.
File renamed without changes.
267 changes: 137 additions & 130 deletions rewriter/native/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),

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(),

capture_errors: true,
do_sourcemaps: true,
scramitize: false,
strict_rewrites: true,
},
)
.context("failed to rewrite file")
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(),

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")
}

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 res = 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()));
}

println!("changes: {:#?}", res.changes);

println!(
"rewritten:\n{}",
String::from_utf8(res.js).context("failed to parse rewritten js")?
);

Ok(())
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();

if bench {
loop {
dorewrite(&data)?;
}
}

let res = 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()));
}

println!("changes: {:#?}", res.changes);

println!(
"rewritten:\n{}",
String::from_utf8(res.js).context("failed to parse rewritten js")?
);

Ok(())
}

#[cfg(test)]
mod test {
use std::fs;

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

use crate::dorewrite;

#[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();

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 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();

let result = context
.eval(Source::from_bytes(
br#"
use std::fs;

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

use crate::dorewrite;

#[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();

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 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();

let result = context
.eval(Source::from_bytes(
br#"
function $wrap(val) {
if (val === window || val === "location" || val === globalThis) return "";
Expand All @@ -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");
}
}
}
1 change: 0 additions & 1 deletion rewriter/rewriter/src/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 2f7d2fa

Please sign in to comment.