Skip to content

Commit

Permalink
Merge pull request #118 from jameslahm/refactor-js-class
Browse files Browse the repository at this point in the history
Refactor use Js Builtins
  • Loading branch information
playXE authored Jul 23, 2021
2 parents 39e3cd1 + 6a6d7d2 commit b01ed1a
Show file tree
Hide file tree
Showing 30 changed files with 1,047 additions and 1,102 deletions.
9 changes: 4 additions & 5 deletions crates/starlight/src/bin/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ impl ClassConstructor for Person {
age: args.at(0).get_int32(),
})
}
fn init(builder: &mut ClassBuilder) -> Result<(), JsValue> {
builder.method("sayHello", Person::say_hello, 0)?;
Ok(())
}
}

impl Person {
Expand All @@ -41,11 +45,6 @@ impl JsClass for Person {
fn class() -> &'static starlight::prelude::Class {
define_jsclass!(Person, Person)
}

fn init(builder: &mut ClassBuilder) -> Result<(), JsValue> {
builder.method("sayHello", Person::say_hello, 0)?;
Ok(())
}
}

fn main() {
Expand Down
50 changes: 35 additions & 15 deletions crates/starlight/src/bin/prop.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
use std::fs::read_to_string;

use starlight::{prelude::Options, Platform};
use starlight::{
prelude::{JsHint, JsValue, Options},
Platform,
};

fn main() {
Platform::initialize();
let mut runtime = Platform::new_runtime(Options::default(), None);
let ctx = runtime.new_context();

let content = read_to_string("examples/hello-world.js").unwrap();
let res = ctx.eval_internal(None, false, &content, false);

match res {
Ok(val) => {
val.to_string(ctx).unwrap_or_else(|_| String::new());
}
Err(e) => println!(
"Uncaught {}",
e.to_string(ctx).unwrap_or_else(|_| String::new())
),
};
let mut ctx = runtime.new_context();

let prototype = ctx
.global_object()
.get(ctx, "Object")
.unwrap()
.to_object(ctx)
.unwrap()
.get(ctx, "prototype")
.unwrap();
let func = prototype
.to_object(ctx)
.unwrap()
.get(ctx, "hasOwnProperty")
.unwrap()
.to_object(ctx)
.unwrap();

let structure = ctx.global_data().get_function_struct();
let prototype = structure.prototype();

println!("{:?}", JsValue::new(structure));
println!("{:?}", JsValue::new(*prototype.unwrap()));

let structure = func.structure();
let prototype = structure.prototype();

println!("{:?}", JsValue::new(structure));

println!("{:?}", JsValue::new(*prototype.unwrap()));
ctx.eval("print(Object.prototype.hasOwnProperty)").unwrap();
}
Loading

0 comments on commit b01ed1a

Please sign in to comment.