1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
//! This module implements the global `Function` object as well as creates Native Functions.
//!
//! Objects wrap `Function`s and expose them via call/construct slots.
//!
//! `The `Function` object is used for matching text with a pattern.
//!
//! More information:
//!  - [ECMAScript reference][spec]
//!  - [MDN documentation][mdn]
//!
//! [spec]: https://tc39.es/ecma262/#sec-function-objects
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function

use crate::{
    builtins::{
        array::Array,
        object::{Object, ObjectData, INSTANCE_PROTOTYPE, PROTOTYPE},
        property::Property,
        value::{RcString, ResultValue, Value},
    },
    environment::function_environment_record::BindingStatus,
    environment::lexical_environment::{new_function_environment, Environment},
    exec::{Executable, Interpreter},
    syntax::ast::node::{FormalParameter, StatementList},
    BoaProfiler,
};
use gc::{unsafe_empty_trace, Finalize, Trace};
use std::fmt::{self, Debug};

/// _fn(this, arguments, ctx) -> ResultValue_ - The signature of a built-in function
pub type NativeFunctionData = fn(&Value, &[Value], &mut Interpreter) -> ResultValue;

/// Sets the ConstructorKind
#[derive(Debug, Copy, Clone)]
pub enum ConstructorKind {
    Base,
    Derived,
}

/// Defines how this references are interpreted within the formal parameters and code body of the function.
///
/// Arrow functions don't define a `this` and thus are lexical, `function`s do define a this and thus are NonLexical

#[derive(Debug, Copy, Finalize, Clone, PartialEq, PartialOrd, Hash)]
pub enum ThisMode {
    Lexical,
    NonLexical,
}

unsafe impl Trace for ThisMode {
    unsafe_empty_trace!();
}

/// FunctionBody is specific to this interpreter, it will either be Rust code or JavaScript code (AST Node)
#[derive(Clone, Finalize)]
pub enum FunctionBody {
    BuiltIn(NativeFunctionData),
    Ordinary(StatementList),
}

impl Debug for FunctionBody {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::BuiltIn(_) => write!(f, "[native]"),
            Self::Ordinary(statements) => write!(f, "{:?}", statements),
        }
    }
}

impl PartialEq for FunctionBody {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::BuiltIn(a), Self::BuiltIn(b)) => std::ptr::eq(a, b),
            (Self::Ordinary(a), Self::Ordinary(b)) => a == b,
            (_, _) => false,
        }
    }
}

impl Eq for FunctionBody {}

/// `Trace` implementation for `FunctionBody`.
///
/// This is indeed safe, but we need to mark this as an empty trace because neither
// `NativeFunctionData` nor Node hold any GC'd objects, but Gc doesn't know that. So we need to
/// signal it manually. `rust-gc` does not have a `Trace` implementation for `fn(_, _, _)`.
///
/// <https://github.com/Manishearth/rust-gc/blob/master/gc/src/trace.rs>
unsafe impl Trace for FunctionBody {
    unsafe_empty_trace!();
}

/// Boa representation of a Function Object.
///
/// <https://tc39.es/ecma262/#sec-ecmascript-function-objects>
#[derive(Trace, Finalize, Clone)]
pub struct Function {
    /// Call/Construct Function body
    pub body: FunctionBody,
    /// Formal Paramaters
    pub params: Box<[FormalParameter]>,
    /// This Mode
    pub this_mode: ThisMode,
    // Environment, built-in functions don't need Environments
    pub environment: Option<Environment>,
    /// Is it constructable
    constructable: bool,
    /// Is it callable.
    callable: bool,
}

impl Function {
    pub fn new<P>(
        parameter_list: P,
        scope: Option<Environment>,
        body: FunctionBody,
        this_mode: ThisMode,
        constructable: bool,
        callable: bool,
    ) -> Self
    where
        P: Into<Box<[FormalParameter]>>,
    {
        Self {
            body,
            environment: scope,
            params: parameter_list.into(),
            this_mode,
            constructable,
            callable,
        }
    }

    /// This will create an ordinary function object
    ///
    /// <https://tc39.es/ecma262/#sec-ordinaryfunctioncreate>
    pub fn ordinary<P>(
        parameter_list: P,
        scope: Environment,
        body: StatementList,
        this_mode: ThisMode,
    ) -> Self
    where
        P: Into<Box<[FormalParameter]>>,
    {
        Self::new(
            parameter_list.into(),
            Some(scope),
            FunctionBody::Ordinary(body),
            this_mode,
            true,
            true,
        )
    }

    /// This will create a built-in function object
    ///
    /// <https://tc39.es/ecma262/#sec-createbuiltinfunction>
    pub fn builtin<P>(parameter_list: P, body: NativeFunctionData) -> Self
    where
        P: Into<Box<[FormalParameter]>>,
    {
        let _timer = BoaProfiler::global().start_event("function::builtin", "function");
        Self::new(
            parameter_list.into(),
            None,
            FunctionBody::BuiltIn(body),
            ThisMode::NonLexical,
            false,
            true,
        )
    }

    /// This will handle calls for both ordinary and built-in functions
    ///
    /// <https://tc39.es/ecma262/#sec-prepareforordinarycall>
    /// <https://tc39.es/ecma262/#sec-ecmascript-function-objects-call-thisargument-argumentslist>
    pub fn call(
        &self,
        function: Value, // represents a pointer to this function object wrapped in a GC (not a `this` JS object)
        this: &Value,
        args_list: &[Value],
        interpreter: &mut Interpreter,
    ) -> ResultValue {
        let _timer = BoaProfiler::global().start_event("function::call", "function");
        if self.callable {
            match self.body {
                FunctionBody::BuiltIn(func) => func(this, args_list, interpreter),
                FunctionBody::Ordinary(ref body) => {
                    // Create a new Function environment who's parent is set to the scope of the function declaration (self.environment)
                    // <https://tc39.es/ecma262/#sec-prepareforordinarycall>
                    let local_env = new_function_environment(
                        function,
                        if let ThisMode::Lexical = self.this_mode {
                            None
                        } else {
                            Some(this.clone())
                        },
                        self.environment.as_ref().cloned(),
                        // Arrow functions do not have a this binding https://tc39.es/ecma262/#sec-function-environment-records
                        if let ThisMode::Lexical = self.this_mode {
                            BindingStatus::Lexical
                        } else {
                            BindingStatus::Uninitialized
                        },
                    );

                    // Add argument bindings to the function environment
                    for (i, param) in self.params.iter().enumerate() {
                        // Rest Parameters
                        if param.is_rest_param() {
                            self.add_rest_param(param, i, args_list, interpreter, &local_env);
                            break;
                        }

                        let value = args_list.get(i).cloned().unwrap_or_else(Value::undefined);
                        self.add_arguments_to_environment(param, value, &local_env);
                    }

                    // Add arguments object
                    let arguments_obj = create_unmapped_arguments_object(args_list);
                    local_env
                        .borrow_mut()
                        .create_mutable_binding("arguments".to_string(), false);
                    local_env
                        .borrow_mut()
                        .initialize_binding("arguments", arguments_obj);

                    interpreter.realm.environment.push(local_env);

                    // Call body should be set before reaching here
                    let result = body.run(interpreter);

                    // local_env gets dropped here, its no longer needed
                    interpreter.realm.environment.pop();
                    result
                }
            }
        } else {
            panic!("TypeError: class constructors must be invoked with 'new'");
        }
    }

    /// <https://tc39.es/ecma262/#sec-ecmascript-function-objects-construct-argumentslist-newtarget>
    pub fn construct(
        &self,
        function: Value, // represents a pointer to this function object wrapped in a GC (not a `this` JS object)
        this: &Value,
        args_list: &[Value],
        interpreter: &mut Interpreter,
    ) -> ResultValue {
        if self.constructable {
            match self.body {
                FunctionBody::BuiltIn(func) => {
                    func(this, args_list, interpreter)?;
                    Ok(this.clone())
                }
                FunctionBody::Ordinary(ref body) => {
                    // Create a new Function environment who's parent is set to the scope of the function declaration (self.environment)
                    // <https://tc39.es/ecma262/#sec-prepareforordinarycall>
                    let local_env = new_function_environment(
                        function,
                        Some(this.clone()),
                        self.environment.as_ref().cloned(),
                        // Arrow functions do not have a this binding https://tc39.es/ecma262/#sec-function-environment-records
                        if let ThisMode::Lexical = self.this_mode {
                            BindingStatus::Lexical
                        } else {
                            BindingStatus::Uninitialized
                        },
                    );

                    // Add argument bindings to the function environment
                    for (i, param) in self.params.iter().enumerate() {
                        // Rest Parameters
                        if param.is_rest_param() {
                            self.add_rest_param(param, i, args_list, interpreter, &local_env);
                            break;
                        }

                        let value = args_list.get(i).cloned().unwrap_or_else(Value::undefined);
                        self.add_arguments_to_environment(param, value, &local_env);
                    }

                    // Add arguments object
                    let arguments_obj = create_unmapped_arguments_object(args_list);
                    local_env
                        .borrow_mut()
                        .create_mutable_binding("arguments".to_string(), false);
                    local_env
                        .borrow_mut()
                        .initialize_binding("arguments", arguments_obj);

                    interpreter.realm.environment.push(local_env);

                    // Call body should be set before reaching here
                    let _ = body.run(interpreter);

                    // local_env gets dropped here, its no longer needed
                    let binding = interpreter.realm.environment.get_this_binding();
                    Ok(binding)
                }
            }
        } else {
            let name = this.get_field("name").to_string();
            panic!("TypeError: {} is not a constructor", name);
        }
    }

    // Adds the final rest parameters to the Environment as an array
    fn add_rest_param(
        &self,
        param: &FormalParameter,
        index: usize,
        args_list: &[Value],
        interpreter: &mut Interpreter,
        local_env: &Environment,
    ) {
        // Create array of values
        let array = Array::new_array(interpreter).unwrap();
        Array::add_to_array_object(&array, &args_list[index..]).unwrap();

        // Create binding
        local_env
            .borrow_mut()
            .create_mutable_binding(param.name().to_owned(), false);

        // Set Binding to value
        local_env
            .borrow_mut()
            .initialize_binding(param.name(), array);
    }

    // Adds an argument to the environment
    fn add_arguments_to_environment(
        &self,
        param: &FormalParameter,
        value: Value,
        local_env: &Environment,
    ) {
        // Create binding
        local_env
            .borrow_mut()
            .create_mutable_binding(param.name().to_owned(), false);

        // Set Binding to value
        local_env
            .borrow_mut()
            .initialize_binding(param.name(), value);
    }

    /// Returns true if the function object is callable.
    pub fn is_callable(&self) -> bool {
        self.callable
    }

    /// Returns true if the function object is constructable.
    pub fn is_constructable(&self) -> bool {
        self.constructable
    }
}

impl Debug for Function {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{{")?;
        write!(f, "[Not implemented]")?;
        write!(f, "}}")
    }
}

/// Arguments.
///
/// <https://tc39.es/ecma262/#sec-createunmappedargumentsobject>
pub fn create_unmapped_arguments_object(arguments_list: &[Value]) -> Value {
    let len = arguments_list.len();
    let mut obj = Object::default();
    obj.set_internal_slot("ParameterMap", Value::undefined());
    // Set length
    let mut length = Property::default();
    length = length.writable(true).value(Value::from(len));
    // Define length as a property
    obj.define_own_property("length".to_string(), length);
    let mut index: usize = 0;
    while index < len {
        let val = arguments_list.get(index).expect("Could not get argument");
        let mut prop = Property::default();
        prop = prop
            .value(val.clone())
            .enumerable(true)
            .writable(true)
            .configurable(true);

        obj.properties_mut()
            .insert(RcString::from(index.to_string()), prop);
        index += 1;
    }

    Value::from(obj)
}

/// Create new function `[[Construct]]`
///
// This gets called when a new Function() is created.
pub fn make_function(this: &Value, _: &[Value], _: &mut Interpreter) -> ResultValue {
    this.set_data(ObjectData::Function(Function::builtin(
        Vec::new(),
        |_, _, _| Ok(Value::undefined()),
    )));
    Ok(this.clone())
}

pub fn create(global: &Value) -> Value {
    let prototype = Value::new_object(Some(global));

    make_constructor_fn("Function", 1, make_function, global, prototype, true)
}

/// Creates a new constructor function
///
/// This utility function handling linking the new Constructor to the prototype.
/// So far this is only used by internal functions
pub fn make_constructor_fn(
    name: &str,
    length: usize,
    body: NativeFunctionData,
    global: &Value,
    prototype: Value,
    constructable: bool,
) -> Value {
    let _timer =
        BoaProfiler::global().start_event(&format!("make_constructor_fn: {}", name), "init");

    // Create the native function
    let mut function = Function::builtin(Vec::new(), body);
    function.constructable = constructable;

    let mut constructor = Object::function(function);

    // Get reference to Function.prototype
    // Create the function object and point its instance prototype to Function.prototype
    constructor.set_internal_slot(
        INSTANCE_PROTOTYPE,
        global.get_field("Function").get_field(PROTOTYPE),
    );

    let length = Property::new()
        .value(Value::from(length))
        .writable(false)
        .configurable(false)
        .enumerable(false);
    constructor.insert_property("length", length);

    let name = Property::new()
        .value(Value::from(name))
        .writable(false)
        .configurable(false)
        .enumerable(false);
    constructor.insert_property("name", name);

    let constructor = Value::from(constructor);

    prototype
        .as_object_mut()
        .unwrap()
        .insert_field("constructor", constructor.clone());

    constructor
        .as_object_mut()
        .expect("constructor object")
        .insert_field(PROTOTYPE, prototype);

    constructor
}

/// Creates a new member function of a `Object` or `prototype`.
///
/// A function registered using this macro can then be called from Javascript using:
///
/// parent.name()
///
/// See the javascript 'Number.toString()' as an example.
///
/// # Arguments
/// function: The function to register as a built in function.
/// name: The name of the function (how it will be called but without the ()).
/// parent: The object to register the function on, if the global object is used then the function is instead called as name()
///     without requiring the parent, see parseInt() as an example.
/// length: As described at https://tc39.es/ecma262/#sec-function-instances-length, The value of the "length" property is an integer that
///     indicates the typical number of arguments expected by the function. However, the language permits the function to be invoked with
///     some other number of arguments.
///
/// If no length is provided, the length will be set to 0.
pub fn make_builtin_fn<N>(function: NativeFunctionData, name: N, parent: &Value, length: usize)
where
    N: Into<String>,
{
    let name = name.into();
    let _timer = BoaProfiler::global().start_event(&format!("make_builtin_fn: {}", &name), "init");

    let mut function = Object::function(Function::builtin(Vec::new(), function));
    function.insert_field("length", Value::from(length));

    parent
        .as_object_mut()
        .unwrap()
        .insert_field(name, Value::from(function));
}

/// Initialise the `Function` object on the global object.
#[inline]
pub fn init(global: &Value) -> (&str, Value) {
    let _timer = BoaProfiler::global().start_event("function", "init");

    ("Function", create(global))
}