@@ -7,6 +7,106 @@ use techscript_runtime::{
77 RuntimeContext ,
88} ;
99
10+ fn abs ( _ctx : & mut RuntimeContext , args : Vec < RuntimeValue > ) -> Result < RuntimeValue , RuntimeError > {
11+ match & args[ 0 ] {
12+ RuntimeValue :: Int ( i) => Ok ( RuntimeValue :: Int ( i. abs ( ) ) ) ,
13+ RuntimeValue :: Float ( f) => Ok ( RuntimeValue :: Float ( f. abs ( ) ) ) ,
14+ other => Err ( RuntimeError :: new (
15+ RuntimeErrorKind :: TypeMismatch {
16+ expected : "Int or Float" . to_string ( ) ,
17+ found : other. runtime_type ( ) . to_string ( ) ,
18+ } ,
19+ None ,
20+ None ,
21+ ) ) ,
22+ }
23+ }
24+
25+ fn sin ( _ctx : & mut RuntimeContext , args : Vec < RuntimeValue > ) -> Result < RuntimeValue , RuntimeError > {
26+ let f = args[ 0 ] . try_into_float ( ) ?;
27+ Ok ( RuntimeValue :: Float ( f. sin ( ) ) )
28+ }
29+
30+ fn cos ( _ctx : & mut RuntimeContext , args : Vec < RuntimeValue > ) -> Result < RuntimeValue , RuntimeError > {
31+ let f = args[ 0 ] . try_into_float ( ) ?;
32+ Ok ( RuntimeValue :: Float ( f. cos ( ) ) )
33+ }
34+
35+ fn tan ( _ctx : & mut RuntimeContext , args : Vec < RuntimeValue > ) -> Result < RuntimeValue , RuntimeError > {
36+ let f = args[ 0 ] . try_into_float ( ) ?;
37+ Ok ( RuntimeValue :: Float ( f. tan ( ) ) )
38+ }
39+
40+ fn log ( _ctx : & mut RuntimeContext , args : Vec < RuntimeValue > ) -> Result < RuntimeValue , RuntimeError > {
41+ let f = args[ 0 ] . try_into_float ( ) ?;
42+ Ok ( RuntimeValue :: Float ( f. ln ( ) ) )
43+ }
44+
45+ fn exp ( _ctx : & mut RuntimeContext , args : Vec < RuntimeValue > ) -> Result < RuntimeValue , RuntimeError > {
46+ let f = args[ 0 ] . try_into_float ( ) ?;
47+ Ok ( RuntimeValue :: Float ( f. exp ( ) ) )
48+ }
49+
50+ fn sqrt ( _ctx : & mut RuntimeContext , args : Vec < RuntimeValue > ) -> Result < RuntimeValue , RuntimeError > {
51+ let f = args[ 0 ] . try_into_float ( ) ?;
52+ Ok ( RuntimeValue :: Float ( f. sqrt ( ) ) )
53+ }
54+
55+ fn pow ( _ctx : & mut RuntimeContext , args : Vec < RuntimeValue > ) -> Result < RuntimeValue , RuntimeError > {
56+ let base = args[ 0 ] . try_into_float ( ) ?;
57+ let exponent = args[ 1 ] . try_into_float ( ) ?;
58+ Ok ( RuntimeValue :: Float ( base. powf ( exponent) ) )
59+ }
60+
61+ fn floor ( _ctx : & mut RuntimeContext , args : Vec < RuntimeValue > ) -> Result < RuntimeValue , RuntimeError > {
62+ let f = args[ 0 ] . try_into_float ( ) ?;
63+ Ok ( RuntimeValue :: Float ( f. floor ( ) ) )
64+ }
65+
66+ fn ceil ( _ctx : & mut RuntimeContext , args : Vec < RuntimeValue > ) -> Result < RuntimeValue , RuntimeError > {
67+ let f = args[ 0 ] . try_into_float ( ) ?;
68+ Ok ( RuntimeValue :: Float ( f. ceil ( ) ) )
69+ }
70+
71+ fn round ( _ctx : & mut RuntimeContext , args : Vec < RuntimeValue > ) -> Result < RuntimeValue , RuntimeError > {
72+ let f = args[ 0 ] . try_into_float ( ) ?;
73+ Ok ( RuntimeValue :: Float ( f. round ( ) ) )
74+ }
75+
76+ fn random (
77+ _ctx : & mut RuntimeContext ,
78+ _args : Vec < RuntimeValue > ,
79+ ) -> Result < RuntimeValue , RuntimeError > {
80+ use std:: cell:: Cell ;
81+ use std:: time:: SystemTime ;
82+ thread_local ! {
83+ static SEED : Cell <u64 > = Cell :: new( {
84+ SystemTime :: now( )
85+ . duration_since( SystemTime :: UNIX_EPOCH )
86+ . unwrap_or_default( )
87+ . as_nanos( ) as u64
88+ } ) ;
89+ }
90+ let next_seed = SEED . with ( |cell| {
91+ let current = cell. get ( ) ;
92+ let next = current
93+ . wrapping_mul ( 6364136223846793005 )
94+ . wrapping_add ( 1442695040888963407 ) ;
95+ cell. set ( next) ;
96+ next
97+ } ) ;
98+ let rand_float = ( next_seed as f64 ) / ( u64:: MAX as f64 ) ;
99+ Ok ( RuntimeValue :: Float ( rand_float) )
100+ }
101+
102+ fn to_float (
103+ _ctx : & mut RuntimeContext ,
104+ args : Vec < RuntimeValue > ,
105+ ) -> Result < RuntimeValue , RuntimeError > {
106+ let f = args[ 0 ] . try_into_float ( ) ?;
107+ Ok ( RuntimeValue :: Float ( f) )
108+ }
109+
10110impl StdlibRegistry {
11111 pub fn register_math ( & mut self ) {
12112 let mut exports: HashMap < String , Rc < dyn techscript_runtime:: function:: Callable > > =
@@ -17,18 +117,7 @@ impl StdlibRegistry {
17117 Rc :: new ( StdFunction {
18118 name : "abs" . to_string ( ) ,
19119 arity : 1 ,
20- callback : |_ctx, args| match & args[ 0 ] {
21- RuntimeValue :: Int ( i) => Ok ( RuntimeValue :: Int ( i. abs ( ) ) ) ,
22- RuntimeValue :: Float ( f) => Ok ( RuntimeValue :: Float ( f. abs ( ) ) ) ,
23- other => Err ( RuntimeError :: new (
24- RuntimeErrorKind :: TypeMismatch {
25- expected : "Int or Float" . to_string ( ) ,
26- found : other. runtime_type ( ) . to_string ( ) ,
27- } ,
28- None ,
29- None ,
30- ) ) ,
31- } ,
120+ callback : abs,
32121 } ) ,
33122 ) ;
34123
@@ -37,10 +126,7 @@ impl StdlibRegistry {
37126 Rc :: new ( StdFunction {
38127 name : "sin" . to_string ( ) ,
39128 arity : 1 ,
40- callback : |_ctx, args| {
41- let f = args[ 0 ] . try_into_float ( ) ?;
42- Ok ( RuntimeValue :: Float ( f. sin ( ) ) )
43- } ,
129+ callback : sin,
44130 } ) ,
45131 ) ;
46132
@@ -49,10 +135,7 @@ impl StdlibRegistry {
49135 Rc :: new ( StdFunction {
50136 name : "cos" . to_string ( ) ,
51137 arity : 1 ,
52- callback : |_ctx, args| {
53- let f = args[ 0 ] . try_into_float ( ) ?;
54- Ok ( RuntimeValue :: Float ( f. cos ( ) ) )
55- } ,
138+ callback : cos,
56139 } ) ,
57140 ) ;
58141
@@ -61,10 +144,7 @@ impl StdlibRegistry {
61144 Rc :: new ( StdFunction {
62145 name : "tan" . to_string ( ) ,
63146 arity : 1 ,
64- callback : |_ctx, args| {
65- let f = args[ 0 ] . try_into_float ( ) ?;
66- Ok ( RuntimeValue :: Float ( f. tan ( ) ) )
67- } ,
147+ callback : tan,
68148 } ) ,
69149 ) ;
70150
@@ -73,10 +153,7 @@ impl StdlibRegistry {
73153 Rc :: new ( StdFunction {
74154 name : "log" . to_string ( ) ,
75155 arity : 1 ,
76- callback : |_ctx, args| {
77- let f = args[ 0 ] . try_into_float ( ) ?;
78- Ok ( RuntimeValue :: Float ( f. ln ( ) ) )
79- } ,
156+ callback : log,
80157 } ) ,
81158 ) ;
82159
@@ -85,10 +162,7 @@ impl StdlibRegistry {
85162 Rc :: new ( StdFunction {
86163 name : "exp" . to_string ( ) ,
87164 arity : 1 ,
88- callback : |_ctx, args| {
89- let f = args[ 0 ] . try_into_float ( ) ?;
90- Ok ( RuntimeValue :: Float ( f. exp ( ) ) )
91- } ,
165+ callback : exp,
92166 } ) ,
93167 ) ;
94168
@@ -97,10 +171,7 @@ impl StdlibRegistry {
97171 Rc :: new ( StdFunction {
98172 name : "sqrt" . to_string ( ) ,
99173 arity : 1 ,
100- callback : |_ctx, args| {
101- let f = args[ 0 ] . try_into_float ( ) ?;
102- Ok ( RuntimeValue :: Float ( f. sqrt ( ) ) )
103- } ,
174+ callback : sqrt,
104175 } ) ,
105176 ) ;
106177
@@ -109,11 +180,7 @@ impl StdlibRegistry {
109180 Rc :: new ( StdFunction {
110181 name : "pow" . to_string ( ) ,
111182 arity : 2 ,
112- callback : |_ctx, args| {
113- let base = args[ 0 ] . try_into_float ( ) ?;
114- let exponent = args[ 1 ] . try_into_float ( ) ?;
115- Ok ( RuntimeValue :: Float ( base. powf ( exponent) ) )
116- } ,
183+ callback : pow,
117184 } ) ,
118185 ) ;
119186
@@ -122,10 +189,7 @@ impl StdlibRegistry {
122189 Rc :: new ( StdFunction {
123190 name : "floor" . to_string ( ) ,
124191 arity : 1 ,
125- callback : |_ctx, args| {
126- let f = args[ 0 ] . try_into_float ( ) ?;
127- Ok ( RuntimeValue :: Float ( f. floor ( ) ) )
128- } ,
192+ callback : floor,
129193 } ) ,
130194 ) ;
131195
@@ -134,10 +198,7 @@ impl StdlibRegistry {
134198 Rc :: new ( StdFunction {
135199 name : "ceil" . to_string ( ) ,
136200 arity : 1 ,
137- callback : |_ctx, args| {
138- let f = args[ 0 ] . try_into_float ( ) ?;
139- Ok ( RuntimeValue :: Float ( f. ceil ( ) ) )
140- } ,
201+ callback : ceil,
141202 } ) ,
142203 ) ;
143204
@@ -146,10 +207,7 @@ impl StdlibRegistry {
146207 Rc :: new ( StdFunction {
147208 name : "round" . to_string ( ) ,
148209 arity : 1 ,
149- callback : |_ctx, args| {
150- let f = args[ 0 ] . try_into_float ( ) ?;
151- Ok ( RuntimeValue :: Float ( f. round ( ) ) )
152- } ,
210+ callback : round,
153211 } ) ,
154212 ) ;
155213
@@ -158,28 +216,7 @@ impl StdlibRegistry {
158216 Rc :: new ( StdFunction {
159217 name : "random" . to_string ( ) ,
160218 arity : 0 ,
161- callback : |_ctx, _args| {
162- use std:: cell:: Cell ;
163- use std:: time:: SystemTime ;
164- thread_local ! {
165- static SEED : Cell <u64 > = Cell :: new( {
166- SystemTime :: now( )
167- . duration_since( SystemTime :: UNIX_EPOCH )
168- . unwrap_or_default( )
169- . as_nanos( ) as u64
170- } ) ;
171- }
172- let next_seed = SEED . with ( |cell| {
173- let current = cell. get ( ) ;
174- let next = current
175- . wrapping_mul ( 6364136223846793005 )
176- . wrapping_add ( 1442695040888963407 ) ;
177- cell. set ( next) ;
178- next
179- } ) ;
180- let rand_float = ( next_seed as f64 ) / ( u64:: MAX as f64 ) ;
181- Ok ( RuntimeValue :: Float ( rand_float) )
182- } ,
219+ callback : random,
183220 } ) ,
184221 ) ;
185222
@@ -188,10 +225,7 @@ impl StdlibRegistry {
188225 Rc :: new ( StdFunction {
189226 name : "to_float" . to_string ( ) ,
190227 arity : 1 ,
191- callback : |_ctx, args| {
192- let f = args[ 0 ] . try_into_float ( ) ?;
193- Ok ( RuntimeValue :: Float ( f) )
194- } ,
228+ callback : to_float,
195229 } ) ,
196230 ) ;
197231
0 commit comments