Skip to content

Commit f3a20a3

Browse files
committed
Refactor and integrate Math module into new engine
- Update src/js_math.rs to use MutationContext and Value types - Implement initialize_math to register Math object and constants - Implement handle_math_call to execute Math functions - Integrate Math initialization in src/core.rs - Add Math function call handling in src/core/eval.rs - Implement Expr::UnaryNeg support in evaluator - Add set_non_writable helper to JSObjectData
1 parent 04387ee commit f3a20a3

File tree

5 files changed

+141
-115
lines changed

5 files changed

+141
-115
lines changed

src/core.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![allow(clippy::collapsible_if, clippy::collapsible_match)]
22

33
use crate::error::JSError;
4+
use crate::js_math::initialize_math;
45
use crate::raise_eval_error;
56
use crate::unicode::utf8_to_utf16;
67
use gc_arena::Mutation as MutationContext;
@@ -56,6 +57,8 @@ pub fn initialize_global_constructors<'gc>(mc: &MutationContext<'gc>, env: &JSOb
5657

5758
initialize_console(mc, env)?;
5859

60+
initialize_math(mc, env)?;
61+
5962
env_set(mc, env, "undefined", Value::Undefined)?;
6063
env_set(mc, env, "NaN", Value::Number(f64::NAN))?;
6164
env_set(mc, env, "Infinity", Value::Number(f64::INFINITY))?;

src/core/eval.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{
77
StatementKind, create_error, env_get, env_set, env_set_recursive, is_error, new_js_object_data, obj_get_key_value,
88
obj_set_key_value, value_to_string,
99
},
10+
js_math::handle_math_call,
1011
raise_eval_error, raise_reference_error,
1112
unicode::{utf8_to_utf16, utf16_to_utf8},
1213
};
@@ -625,6 +626,9 @@ pub fn evaluate_expr<'gc>(mc: &MutationContext<'gc>, env: &JSObjectDataPtr<'gc>,
625626
.join(" ");
626627
println!("{}", output);
627628
Ok(Value::Undefined)
629+
} else if name.starts_with("Math.") {
630+
let method = &name[5..];
631+
Ok(handle_math_call(mc, method, &eval_args, env).map_err(EvalError::Js)?)
628632
} else if name == "console.error" {
629633
let output = eval_args
630634
.iter()
@@ -839,6 +843,14 @@ pub fn evaluate_expr<'gc>(mc: &MutationContext<'gc>, env: &JSObjectDataPtr<'gc>,
839843
}
840844
Ok(Value::String(result))
841845
}
846+
Expr::UnaryNeg(expr) => {
847+
let val = evaluate_expr(mc, env, expr)?;
848+
if let Value::Number(n) = val {
849+
Ok(Value::Number(-n))
850+
} else {
851+
Err(EvalError::Js(raise_eval_error!("Unary Negation only for numbers")))
852+
}
853+
}
842854
_ => Ok(Value::Undefined),
843855
}
844856
}

src/core/value.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ impl<'gc> JSObjectData<'gc> {
160160
pub fn set_non_configurable(&mut self, key: PropertyKey<'gc>) {
161161
self.non_configurable.insert(key);
162162
}
163+
pub fn set_non_writable(&mut self, key: PropertyKey<'gc>) {
164+
self.non_writable.insert(key);
165+
}
163166
pub fn is_const(&self, key: &str) -> bool {
164167
self.constants.contains(key)
165168
}

0 commit comments

Comments
 (0)