diff options
author | Andreas Kling <kling@serenityos.org> | 2020-04-27 12:56:09 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-04-28 15:07:08 +0200 |
commit | ee0bf55127dc2add3b271d11c22395f7e29b1860 (patch) | |
tree | f1ed51427d1c26576a6f26ce790cfecfb3a449f2 /Libraries/LibJS/Runtime | |
parent | 3c4a9e421f7465d13da256211414957a9d8650ec (diff) | |
download | serenity-ee0bf55127dc2add3b271d11c22395f7e29b1860.zip |
LibJS: Make AssignmentExpression assign through a Reference
Reference now has assign(Interpreter&, Value) which is used to write
transparently through a Reference into whatever location it refers to.
Diffstat (limited to 'Libraries/LibJS/Runtime')
-rw-r--r-- | Libraries/LibJS/Runtime/Reference.cpp | 51 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Reference.h | 2 |
2 files changed, 53 insertions, 0 deletions
diff --git a/Libraries/LibJS/Runtime/Reference.cpp b/Libraries/LibJS/Runtime/Reference.cpp new file mode 100644 index 0000000000..13402cdc9d --- /dev/null +++ b/Libraries/LibJS/Runtime/Reference.cpp @@ -0,0 +1,51 @@ +/* + * 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. + */ + +#include <LibJS/Interpreter.h> +#include <LibJS/Runtime/Reference.h> +#include <LibJS/Runtime/Object.h> + +namespace JS { + +void Reference::assign(Interpreter& interpreter, Value value) +{ + // NOTE: The caller is responsible for doing an exception check after assign(). + + ASSERT(!is_unresolvable()); + + if (is_local_variable()) { + interpreter.set_variable(m_name.to_string(), value); + return; + } + + auto* object = base().to_object(interpreter.heap()); + if (!object) + return; + + object->put(m_name, value); +} + +} diff --git a/Libraries/LibJS/Runtime/Reference.h b/Libraries/LibJS/Runtime/Reference.h index 2cad110497..81b1701496 100644 --- a/Libraries/LibJS/Runtime/Reference.h +++ b/Libraries/LibJS/Runtime/Reference.h @@ -71,6 +71,8 @@ public: return m_local_variable; } + void assign(Interpreter&, Value); + private: Value m_base { js_undefined() }; PropertyName m_name; |