2020-03-12 19:53:31 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Function.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
#include <LibJS/Runtime/Function.h>
|
2020-03-12 19:53:31 +01:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2020-03-28 17:23:54 +01:00
|
|
|
class NativeFunction : public Function {
|
2020-06-21 15:14:02 +02:00
|
|
|
JS_OBJECT(NativeFunction, Function);
|
|
|
|
|
2020-03-12 19:53:31 +01:00
|
|
|
public:
|
2020-06-20 13:55:34 +02:00
|
|
|
static NativeFunction* create(Interpreter&, GlobalObject&, const FlyString& name, AK::Function<Value(Interpreter&, GlobalObject&)>);
|
2020-04-17 19:59:32 +02:00
|
|
|
|
2020-06-20 13:55:34 +02:00
|
|
|
explicit NativeFunction(const FlyString& name, AK::Function<Value(Interpreter&, GlobalObject&)>, Object& prototype);
|
2020-06-20 15:40:48 +02:00
|
|
|
virtual void initialize(Interpreter&, GlobalObject&) override { }
|
2020-03-12 19:53:31 +01:00
|
|
|
virtual ~NativeFunction() override;
|
|
|
|
|
2020-03-28 22:48:35 +01:00
|
|
|
virtual Value call(Interpreter&) override;
|
2020-06-25 15:30:58 -07:00
|
|
|
virtual Value construct(Interpreter&, Function& new_target) override;
|
2020-04-01 18:31:24 +01:00
|
|
|
|
2020-04-11 12:56:20 +01:00
|
|
|
virtual const FlyString& name() const override { return m_name; };
|
2020-04-01 18:31:24 +01:00
|
|
|
virtual bool has_constructor() const { return false; }
|
2020-03-12 19:53:31 +01:00
|
|
|
|
2020-03-28 17:23:54 +01:00
|
|
|
protected:
|
2020-04-17 19:59:32 +02:00
|
|
|
NativeFunction(const FlyString& name, Object& prototype);
|
|
|
|
explicit NativeFunction(Object& prototype);
|
2020-03-28 17:23:54 +01:00
|
|
|
|
2020-03-12 19:53:31 +01:00
|
|
|
private:
|
|
|
|
virtual bool is_native_function() const override { return true; }
|
2020-06-08 13:31:21 -05:00
|
|
|
virtual LexicalEnvironment* create_environment() override final;
|
2020-03-12 19:53:31 +01:00
|
|
|
|
2020-04-11 12:56:20 +01:00
|
|
|
FlyString m_name;
|
2020-06-20 13:55:34 +02:00
|
|
|
AK::Function<Value(Interpreter&, GlobalObject&)> m_native_function;
|
2020-03-12 19:53:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|