blob: d3615379d4301c80d2a8aa4d0909dfc8915e05df [file] [log] [blame]
mjs6f821c82002-03-22 00:31:57 +00001/*
mjs6f821c82002-03-22 00:31:57 +00002 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
mark.lam@apple.com30721252013-11-21 05:29:42 +00004 * Copyright (C) 2003, 2007, 2013 Apple Inc.
mjs6f821c82002-03-22 00:31:57 +00005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
mjscdff33b2006-01-23 21:41:36 +000018 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
ggaren07d4ce62005-07-14 18:27:04 +000019 * Boston, MA 02110-1301, USA.
mjs6f821c82002-03-22 00:31:57 +000020 *
mjs6f821c82002-03-22 00:31:57 +000021 */
22
mjsb64c50a2005-10-03 21:13:12 +000023#include "config.h"
cwzwarich@webkit.org77647132008-11-05 23:48:18 +000024#include "Completion.h"
ggaren@apple.com4240a312008-11-17 17:55:40 +000025
ggaren@apple.com5169fc92008-11-17 22:11:26 +000026#include "CallFrame.h"
barraclough@apple.comf51cff32012-01-29 03:47:13 +000027#include "CodeProfiling.h"
fpizlo@apple.coma4b4cbe2013-01-12 04:47:03 +000028#include "Debugger.h"
mark.lam@apple.com6ed08272015-06-05 18:52:12 +000029#include "Exception.h"
fpizlo@apple.coma4b4cbe2013-01-12 04:47:03 +000030#include "Interpreter.h"
mjs07781952007-10-25 07:16:49 +000031#include "JSGlobalObject.h"
ap@webkit.org01aff702008-08-20 07:23:06 +000032#include "JSLock.h"
fpizlo@apple.comfb7eff22014-02-11 01:45:50 +000033#include "JSCInlines.h"
ggaren@apple.com898e9cd2007-12-08 00:35:08 +000034#include "Parser.h"
eric@webkit.org3979f2d2012-03-07 08:50:54 +000035#include <wtf/WTFThreadData.h>
mjs3bfb61b2006-03-02 09:12:06 +000036
cwzwarich@webkit.org3f782f62008-09-08 01:28:33 +000037namespace JSC {
mjs6f821c82002-03-22 00:31:57 +000038
weinig@apple.comf41a7da2011-09-08 22:38:44 +000039bool checkSyntax(ExecState* exec, const SourceCode& source, JSValue* returnedException)
mjs6f821c82002-03-22 00:31:57 +000040{
mhahnenberg@apple.come16f8092012-06-27 23:08:26 +000041 JSLockHolder lock(exec);
barraclough@apple.com0cde90d2014-03-20 21:05:49 +000042 RELEASE_ASSERT(exec->vm().atomicStringTable() == wtfThreadData().atomicStringTable());
ap@webkit.org01aff702008-08-20 07:23:06 +000043
oliver@apple.comba10bec2011-03-08 23:17:32 +000044 ProgramExecutable* program = ProgramExecutable::create(exec, source);
oliver@apple.comebd0b4c2009-09-29 21:48:52 +000045 JSObject* error = program->checkSyntax(exec);
weinig@apple.comf41a7da2011-09-08 22:38:44 +000046 if (error) {
47 if (returnedException)
48 *returnedException = error;
49 return false;
50 }
mrowe@apple.com2f6dfdf2008-05-22 01:20:45 +000051
weinig@apple.comf41a7da2011-09-08 22:38:44 +000052 return true;
mjs6f821c82002-03-22 00:31:57 +000053}
oliver@apple.com568b48c2013-04-21 23:26:56 +000054
oliver@apple.com93963fe2013-04-25 18:59:50 +000055bool checkSyntax(VM& vm, const SourceCode& source, ParserError& error)
oliver@apple.com568b48c2013-04-21 23:26:56 +000056{
oliver@apple.com93963fe2013-04-25 18:59:50 +000057 JSLockHolder lock(vm);
barraclough@apple.com0cde90d2014-03-20 21:05:49 +000058 RELEASE_ASSERT(vm.atomicStringTable() == wtfThreadData().atomicStringTable());
ggaren@apple.com8b873e52015-03-17 21:15:03 +000059 return !!parse<ProgramNode>(
saambarati1@gmail.comc497d152015-07-17 18:48:30 +000060 &vm, source, Identifier(), JSParserBuiltinMode::NotBuiltin,
ggaren@apple.com8b873e52015-03-17 21:15:03 +000061 JSParserStrictMode::NotStrict, JSParserCodeType::Program, error);
oliver@apple.com568b48c2013-04-21 23:26:56 +000062}
mjs6f821c82002-03-22 00:31:57 +000063
mark.lam@apple.com6c5e0762015-06-16 20:51:04 +000064JSValue evaluate(ExecState* exec, const SourceCode& source, JSValue thisValue, NakedPtr<Exception>& returnedException)
mjs6f821c82002-03-22 00:31:57 +000065{
mhahnenberg@apple.come16f8092012-06-27 23:08:26 +000066 JSLockHolder lock(exec);
barraclough@apple.com0cde90d2014-03-20 21:05:49 +000067 RELEASE_ASSERT(exec->vm().atomicStringTable() == wtfThreadData().atomicStringTable());
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +000068 RELEASE_ASSERT(!exec->vm().isCollectorBusy());
kmccullough@apple.comac769762008-04-16 19:08:03 +000069
barraclough@apple.comf51cff32012-01-29 03:47:13 +000070 CodeProfiling profile(source);
71
oliver@apple.comba10bec2011-03-08 23:17:32 +000072 ProgramExecutable* program = ProgramExecutable::create(exec, source);
73 if (!program) {
mark.lam@apple.com6ed08272015-06-05 18:52:12 +000074 returnedException = exec->vm().exception();
commit-queue@webkit.org3f922f92013-08-29 00:28:42 +000075 exec->vm().clearException();
weinig@apple.comf41a7da2011-09-08 22:38:44 +000076 return jsUndefined();
oliver@apple.comba10bec2011-03-08 23:17:32 +000077 }
mrowe@apple.com2f6dfdf2008-05-22 01:20:45 +000078
barraclough@apple.com19afece2011-07-15 19:51:49 +000079 if (!thisValue || thisValue.isUndefinedOrNull())
mark.lam@apple.com30721252013-11-21 05:29:42 +000080 thisValue = exec->vmEntryGlobalObject();
oliver@apple.come2fe4ce2013-07-25 03:59:41 +000081 JSObject* thisObj = jsCast<JSObject*>(thisValue.toThis(exec, NotStrictMode));
ggaren@apple.comb11e7872012-08-30 22:50:00 +000082 JSValue result = exec->interpreter()->execute(program, exec, thisObj);
kmccullough@apple.comac769762008-04-16 19:08:03 +000083
barraclough@apple.com2607dd02010-10-27 20:46:09 +000084 if (exec->hadException()) {
mark.lam@apple.com6ed08272015-06-05 18:52:12 +000085 returnedException = exec->exception();
weinig@apple.comf41a7da2011-09-08 22:38:44 +000086 exec->clearException();
87 return jsUndefined();
oliver@apple.comc8f3a752008-06-28 04:02:03 +000088 }
weinig@apple.comf41a7da2011-09-08 22:38:44 +000089
oliver@apple.com903b0c02013-01-24 01:40:37 +000090 RELEASE_ASSERT(result);
weinig@apple.comf41a7da2011-09-08 22:38:44 +000091 return result;
mjs6f821c82002-03-22 00:31:57 +000092}
93
cwzwarich@webkit.org3f782f62008-09-08 01:28:33 +000094} // namespace JSC