1 | /*
|
---|
2 | * Copyright (C) 1999-2001 Harri Porten ([email protected])
|
---|
3 | * Copyright (C) 2001 Peter Kelly ([email protected])
|
---|
4 | * Copyright (C) 2003, 2007 Apple Inc.
|
---|
5 | *
|
---|
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
|
---|
18 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
---|
19 | * Boston, MA 02110-1301, USA.
|
---|
20 | *
|
---|
21 | */
|
---|
22 |
|
---|
23 | #include "config.h"
|
---|
24 | #include "interpreter.h"
|
---|
25 |
|
---|
26 | #include "ExecState.h"
|
---|
27 | #include "JSGlobalObject.h"
|
---|
28 | #include "Machine.h"
|
---|
29 | #include "Parser.h"
|
---|
30 | #include "completion.h"
|
---|
31 | #include "debugger.h"
|
---|
32 | #include <profiler/Profiler.h>
|
---|
33 | #include <stdio.h>
|
---|
34 |
|
---|
35 | #if !PLATFORM(WIN_OS)
|
---|
36 | #include <unistd.h>
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | namespace KJS {
|
---|
40 |
|
---|
41 | Completion Interpreter::checkSyntax(ExecState* exec, const UString& sourceURL, int startingLineNumber, const UString& code)
|
---|
42 | {
|
---|
43 | return checkSyntax(exec, sourceURL, startingLineNumber, UStringSourceProvider::create(code));
|
---|
44 | }
|
---|
45 |
|
---|
46 | Completion Interpreter::checkSyntax(ExecState* exec, const UString& sourceURL, int startingLineNumber, PassRefPtr<SourceProvider> source)
|
---|
47 | {
|
---|
48 | JSLock lock;
|
---|
49 |
|
---|
50 | int errLine;
|
---|
51 | UString errMsg;
|
---|
52 |
|
---|
53 | RefPtr<ProgramNode> progNode = exec->parser()->parse<ProgramNode>(exec, sourceURL, startingLineNumber, source, 0, &errLine, &errMsg);
|
---|
54 | if (!progNode)
|
---|
55 | return Completion(Throw, Error::create(exec, SyntaxError, errMsg, errLine, 0, sourceURL));
|
---|
56 | return Completion(Normal);
|
---|
57 | }
|
---|
58 |
|
---|
59 | Completion Interpreter::evaluate(ExecState* exec, ScopeChain& scopeChain, const UString& sourceURL, int startingLineNumber, const UString& code, JSValue* thisV)
|
---|
60 | {
|
---|
61 | return evaluate(exec, scopeChain, sourceURL, startingLineNumber, UStringSourceProvider::create(code), thisV);
|
---|
62 | }
|
---|
63 |
|
---|
64 | Completion Interpreter::evaluate(ExecState* exec, ScopeChain& scopeChain, const UString& sourceURL, int startingLineNumber, PassRefPtr<SourceProvider> source, JSValue* thisValue)
|
---|
65 | {
|
---|
66 | JSLock lock;
|
---|
67 |
|
---|
68 | // parse the source code
|
---|
69 | int sourceId;
|
---|
70 | int errLine;
|
---|
71 | UString errMsg;
|
---|
72 |
|
---|
73 | RefPtr<ProgramNode> programNode = exec->parser()->parse<ProgramNode>(exec, sourceURL, startingLineNumber, source, &sourceId, &errLine, &errMsg);
|
---|
74 |
|
---|
75 | // no program node means a syntax error occurred
|
---|
76 | if (!programNode)
|
---|
77 | return Completion(Throw, Error::create(exec, SyntaxError, errMsg, errLine, sourceId, sourceURL));
|
---|
78 |
|
---|
79 | JSObject* thisObj = (!thisValue || thisValue->isUndefinedOrNull()) ? exec->dynamicGlobalObject() : thisValue->toObject(exec);
|
---|
80 |
|
---|
81 | JSValue* exception = 0;
|
---|
82 | JSValue* result = exec->machine()->execute(programNode.get(), exec, scopeChain.node(), thisObj, &exception);
|
---|
83 |
|
---|
84 | if (exception) {
|
---|
85 | if (exception->isObject() && static_cast<JSObject*>(exception)->isWatchdogException())
|
---|
86 | return Completion(Interrupted, result);
|
---|
87 | return Completion(Throw, exception);
|
---|
88 | }
|
---|
89 | return Completion(Normal, result);
|
---|
90 | }
|
---|
91 |
|
---|
92 | static bool printExceptions = false;
|
---|
93 |
|
---|
94 | bool Interpreter::shouldPrintExceptions()
|
---|
95 | {
|
---|
96 | return printExceptions;
|
---|
97 | }
|
---|
98 |
|
---|
99 | void Interpreter::setShouldPrintExceptions(bool print)
|
---|
100 | {
|
---|
101 | printExceptions = print;
|
---|
102 | }
|
---|
103 |
|
---|
104 | } // namespace KJS
|
---|