/usr/include/KF5/kjs/interpreter.h is in libkf5kjs-dev 5.28.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 | /*
* This file is part of the KDE libraries
* Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
* Copyright (C) 2001 Peter Kelly (pmk@post.com)
* Copyright (C) 2003 Apple Computer, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#ifndef _KJS_INTERPRETER_H_
#define _KJS_INTERPRETER_H_
#include "ExecState.h"
#include "protect.h"
#include "value.h"
#include "types.h"
#include <wtf/HashMap.h>
namespace KJS
{
class Debugger;
class SavedBuiltins;
class TimeoutChecker;
class Package;
class ActivationImp;
class JSGlobalObject;
class StringImp;
#if USE(BINDINGS)
namespace Bindings
{
class RootObject;
}
#endif
/**
* Interpreter objects can be used to evaluate ECMAScript code. Each
* interpreter has a global object which is used for the purposes of code
* evaluation, and also provides access to built-in properties such as
* " Object" and "Number".
*/
class KJS_EXPORT Interpreter
{
friend class Collector;
friend class TimeoutChecker;
public:
/**
* Creates a new interpreter. The supplied object will be used as the global
* object for all scripts executed with this interpreter. During
* construction, all the standard properties such as "Object" and "Number"
* will be added to the global object.
*
* Note: You should not use the same global object for multiple
* interpreters.
*
* This is due do the fact that the built-in properties are set in the
* constructor, and if these objects have been modified from another
* interpreter (e.g. a script modifying String.prototype), the changes will
* be overridden.
*
* @param globalObject The object to use as the global object for this interpreter
*/
Interpreter(JSGlobalObject *globalObject);
/**
* Creates a new interpreter. A global object will be created and
* initialized with the standard global properties.
*/
Interpreter();
/**
* Returns the object that is used as the global object during all script
* execution performed by this interpreter
*/
JSGlobalObject *globalObject() const;
void initGlobalObject();
/**
* Returns the execution state object which can be used to execute
* scripts using this interpreter at a the "global" level, i.e. one
* with a execution context that has the global object as the "this"
* value, and who's scope chain contains only the global object.
*
* Note: this pointer remains constant for the life of the interpreter
* and should not be manually deleted.
*
* @return The interpreter global execution state object
*/
virtual ExecState *globalExec();
/**
* Sets the package instance that will be used to resolve the
* first level of identifiers of import statements.
*
* If no package is set which will make any "import" script
* statement fail with an error. This is the default in e.g. a
* Web browser where package imports should be disabled for
* security reasons.
*/
void setGlobalPackage(Package *p);
/**
* Returns the package that was installed to handle top level
* package requests. Returns 0, the default, when no package was
* set.
*
* @return The global package
*/
Package *globalPackage();
/**
* Parses the supplied ECMAScript code and checks for syntax errors.
*
* @param code The code to check
* @param sourceURL A URL denoting the origin of the code
* @param startingLineNumber The line offset within an embedding context
* @return A normal completion if there were no syntax errors in the code,
* otherwise a throw completion with the syntax error as its value.
*/
Completion checkSyntax(const UString &sourceURL, int startingLineNumber, const UString &code);
Completion checkSyntax(const UString &sourceURL, int startingLineNumber, const UChar *code, int codeLength);
/**
* Evaluates the supplied ECMAScript code.
*
* Since this method returns a Completion, you should check the type of
* completion to detect an error or before attempting to access the returned
* value. For example, if an error occurs during script execution and is not
* caught by the script, the completion type will be Throw.
*
* If the supplied code is invalid, a SyntaxError will be thrown.
*
* @param sourceURL A URL denoting the origin of the code
* @param startingLineNumber The line offset within an embedding context
* @param code The code to evaluate
* @param codeLength The length of the code to evaluate
* @param thisV The value to pass in as the "this" value for the script
* execution. This should either be jsNull() or an Object.
* @return A completion object representing the result of the execution.
*/
Completion evaluate(const UString &sourceURL, int startingLineNumber, const UChar *code, int codeLength, JSValue *thisV = 0);
Completion evaluate(const UString &sourceURL, int startingLineNumber, const UString &code, JSValue *thisV = 0);
/**
* Pretty-prints the supplied ECMAScript code after checking it
* for syntax errors.
*
* @param sourceURL A URL denoting the origin of the code
* @param startingLineNumber The line offset within an embedding context
* @param codeIn The code to check
* @param codeIn Pointer to string that will contain reformatted code
* upon successful parsing.
* @return A normal completion if there were no syntax errors in the code,
* otherwise a throw completion with the syntax error as its value.
*/
static bool normalizeCode(const UString &codeIn, UString *codeOut,
int *errLine = 0, UString *errMsg = 0);
/**
* Returns the builtin "Object" object. This is the object that was set
* as a property of the global object during construction; if the property
* is replaced by script code, this method will still return the original
* object.
*
* @return The builtin "Object" object
*/
JSObject *builtinObject() const;
/**
* Returns the builtin "Function" object.
*/
JSObject *builtinFunction() const;
/**
* Returns the builtin "Array" object.
*/
JSObject *builtinArray() const;
/**
* Returns the builtin "Boolean" object.
*/
JSObject *builtinBoolean() const;
/**
* Returns the builtin "String" object.
*/
JSObject *builtinString() const;
/**
* Returns the builtin "Number" object.
*/
JSObject *builtinNumber() const;
/**
* Returns the builtin "Date" object.
*/
JSObject *builtinDate() const;
/**
* Returns the builtin "RegExp" object.
*/
JSObject *builtinRegExp() const;
/**
* Returns the builtin "Error" object.
*/
JSObject *builtinError() const;
/**
* Returns the builtin "Object.prototype" object.
*/
JSObject *builtinObjectPrototype() const;
/**
* Returns the builtin "Function.prototype" object.
*/
JSObject *builtinFunctionPrototype() const;
/**
* Returns the builtin "Array.prototype" object.
*/
JSObject *builtinArrayPrototype() const;
/**
* Returns the builtin "Boolean.prototype" object.
*/
JSObject *builtinBooleanPrototype() const;
/**
* Returns the builtin "String.prototype" object.
*/
JSObject *builtinStringPrototype() const;
/**
* Returns the builtin "Number.prototype" object.
*/
JSObject *builtinNumberPrototype() const;
/**
* Returns the builtin "Date.prototype" object.
*/
JSObject *builtinDatePrototype() const;
/**
* Returns the builtin "RegExp.prototype" object.
*/
JSObject *builtinRegExpPrototype() const;
/**
* Returns the builtin "Error.prototype" object.
*/
JSObject *builtinErrorPrototype() const;
/**
* The initial value of "Error" global property
*/
JSObject *builtinEvalError() const;
JSObject *builtinRangeError() const;
JSObject *builtinReferenceError() const;
JSObject *builtinSyntaxError() const;
JSObject *builtinTypeError() const;
JSObject *builtinURIError() const;
JSObject *builtinEvalErrorPrototype() const;
JSObject *builtinRangeErrorPrototype() const;
JSObject *builtinReferenceErrorPrototype() const;
JSObject *builtinSyntaxErrorPrototype() const;
JSObject *builtinTypeErrorPrototype() const;
JSObject *builtinURIErrorPrototype() const;
enum CompatMode { NativeMode, IECompat, NetscapeCompat };
/**
* Call this to enable a compatibility mode with another browser.
* (by default konqueror is in "native mode").
* Currently, in KJS, this only changes the behavior of Date::getYear()
* which returns the full year under IE.
*/
void setCompatMode(CompatMode mode)
{
m_compatMode = mode;
}
CompatMode compatMode() const
{
return m_compatMode;
}
/**
* Run the garbage collection. Returns true when at least one object
* was collected; false otherwise.
*/
static bool collect();
/**
* Called during the mark phase of the garbage collector. Subclasses
* implementing custom mark methods must make sure to chain to this one.
*/
virtual void mark(bool currentThreadIsMainThread);
/**
* This marks all GC heap resources stored as optimizations;
* and which have their lifetime managed by the appropriate AST.
* It's static since code can survive the interpreter by a bit.
*/
static void markSourceCachedObjects();
/**
* Provides a way to distinguish derived classes.
* Only useful if you reimplement Interpreter and if different kind of
* interpreters are created in the same process.
* The base class returns 0, the ECMA-bindings interpreter returns 1.
*/
virtual int rtti()
{
return 0;
}
static bool shouldPrintExceptions();
static void setShouldPrintExceptions(bool);
void saveBuiltins(SavedBuiltins &) const;
void restoreBuiltins(const SavedBuiltins &);
/**
* Determine if the it is 'safe' to execute code in the target interpreter from an
* object that originated in this interpreter. This check is used to enforce WebCore
* cross frame security rules. In particular, attempts to access 'bound' objects are
* not allowed unless isSafeScript returns true.
*/
virtual bool isSafeScript(const Interpreter *)
{
return true;
}
#if USE(BINDINGS)
virtual void *createLanguageInstanceForValue(ExecState *, int language, JSObject *value, const Bindings::RootObject *origin, const Bindings::RootObject *current);
#endif
// Chained list of interpreters (ring)
static Interpreter *firstInterpreter()
{
return s_hook;
}
Interpreter *nextInterpreter() const
{
return next;
}
Interpreter *prevInterpreter() const
{
return prev;
}
Debugger *debugger() const
{
return m_debugger;
}
void setDebugger(Debugger *d)
{
m_debugger = d;
}
void setExecState(ExecState *e)
{
m_execState = e;
}
// Note: may be 0, if in globalExec
ExecState *execState()
{
return m_execState ? m_execState : &m_globalExec;
}
void setTimeoutTime(unsigned timeoutTime)
{
m_timeoutTime = timeoutTime;
}
void startTimeoutCheck();
void stopTimeoutCheck();
// Resets the timer to full time if it's running
void restartTimeoutCheck();
void pauseTimeoutCheck();
void resumeTimeoutCheck();
bool checkTimeout();
void ref()
{
++m_refCount;
}
void deref()
{
if (--m_refCount <= 0) {
delete this;
}
}
int refCount() const
{
return m_refCount;
}
unsigned char *stackAlloc(size_t size)
{
unsigned char *nextPtr = stackPtr + size;
if (nextPtr <= stackEnd) {
unsigned char *toRet = stackPtr;
stackPtr = nextPtr;
return toRet;
}
return extendStack(size);
}
void stackFree(size_t size)
{
stackPtr -= size; // ### shrink it?
}
ActivationImp *getRecycledActivation()
{
ActivationImp *out = 0;
if (m_numCachedActivations) {
m_numCachedActivations--;
out = m_cachedActivations[m_numCachedActivations];
}
return out;
}
void recycleActivation(ActivationImp *act);
// Global string table management. This is used from StringNode
// to cache StringImp's for string literals. We keep refcounts
// to permit multiple ones to use the same value.
static StringImp *internString(const UString &literal);
static void releaseInternedString(const UString &literal);
typedef WTF::HashMap<UString::Rep *, std::pair<KJS::StringImp *, int> > InternedStringsTable;
private:
static void markInternedStringsTable();
// This creates a table if needed
static void initInternedStringsTable();
static InternedStringsTable *s_internedStrings;
protected:
virtual ~Interpreter(); // only deref should delete us
virtual bool shouldInterruptScript() const
{
return true;
}
long m_timeoutTime;
private:
bool handleTimeout();
void init();
void printException(const Completion &c, const UString &sourceURL);
/**
* This constructor is not implemented, in order to prevent
* copy-construction of Interpreter objects. You should always pass around
* pointers to an interpreter instance instead.
*/
Interpreter(const Interpreter &);
/**
* This operator is not implemented, in order to prevent assignment of
* Interpreter objects. You should always pass around pointers to an
* interpreter instance instead.
*/
Interpreter operator=(const Interpreter &);
int m_refCount;
JSGlobalObject *m_globalObject;
GlobalExecState m_globalExec;
Package *globPkg;
// Execution stack stuff for this interpreter.
unsigned char *stackBase; // lowest address in the array
unsigned char *stackPtr; // current top/next to allocate
unsigned char *stackEnd; // last address in the stack
unsigned char *extendStack(size_t needed);
// A list of cached activations
enum {MaxCachedActivations = 32};
ActivationImp *m_cachedActivations[MaxCachedActivations];
int m_numCachedActivations;
// Chained list of interpreters (ring) - for collector
static Interpreter *s_hook;
Interpreter *next, *prev;
int m_recursion;
Debugger *m_debugger;
ExecState *m_execState;
CompatMode m_compatMode;
TimeoutChecker *m_timeoutChecker;
bool m_timedOut;
unsigned m_startTimeoutCheckCount;
unsigned m_pauseTimeoutCheckCount;
// Helper for setting constructors, making sure their function names are OK
void putNamedConstructor(const char *name, JSObject *value);
ProtectedPtr<JSObject> m_Object;
ProtectedPtr<JSObject> m_Function;
ProtectedPtr<JSObject> m_Array;
ProtectedPtr<JSObject> m_Boolean;
ProtectedPtr<JSObject> m_String;
ProtectedPtr<JSObject> m_Number;
ProtectedPtr<JSObject> m_Date;
ProtectedPtr<JSObject> m_RegExp;
ProtectedPtr<JSObject> m_Error;
ProtectedPtr<JSObject> m_ObjectPrototype;
ProtectedPtr<JSObject> m_FunctionPrototype;
ProtectedPtr<JSObject> m_ArrayPrototype;
ProtectedPtr<JSObject> m_BooleanPrototype;
ProtectedPtr<JSObject> m_StringPrototype;
ProtectedPtr<JSObject> m_NumberPrototype;
ProtectedPtr<JSObject> m_DatePrototype;
ProtectedPtr<JSObject> m_RegExpPrototype;
ProtectedPtr<JSObject> m_ErrorPrototype;
ProtectedPtr<JSObject> m_EvalError;
ProtectedPtr<JSObject> m_RangeError;
ProtectedPtr<JSObject> m_ReferenceError;
ProtectedPtr<JSObject> m_SyntaxError;
ProtectedPtr<JSObject> m_TypeError;
ProtectedPtr<JSObject> m_UriError;
ProtectedPtr<JSObject> m_EvalErrorPrototype;
ProtectedPtr<JSObject> m_RangeErrorPrototype;
ProtectedPtr<JSObject> m_ReferenceErrorPrototype;
ProtectedPtr<JSObject> m_SyntaxErrorPrototype;
ProtectedPtr<JSObject> m_TypeErrorPrototype;
ProtectedPtr<JSObject> m_UriErrorPrototype;
};
inline bool Interpreter::checkTimeout()
{
if (!m_timedOut) {
return false;
}
return handleTimeout();
}
/**
* Interface to set enhanced Unicode support functions. By default
* the interpreter will use the standard C library functions.
*
* @internal
*/
class KJS_EXPORT UnicodeSupport
{
public:
UnicodeSupport();
typedef bool (*CharCategoryFunction)(int c);
static void setIdentStartChecker(CharCategoryFunction f);
static void setIdentPartChecker(CharCategoryFunction f);
typedef int (*StringConversionFunction)(uint16_t *str, int strLength,
uint16_t *&destIfNeeded);
static void setToLowerFunction(StringConversionFunction f);
static void setToUpperFunction(StringConversionFunction f);
};
/**
* Define a Qt-based version of the Unicode support functions.
*
* @internal
*/
#define KJS_QT_UNICODE_IMPL \
namespace KJS { \
static bool qtIdentStart(int c) { if (c & 0xffff0000) return false; QChar::Category cat = QChar((unsigned short)c).category(); return cat == QChar::Letter_Uppercase || cat == QChar::Letter_Lowercase || cat == QChar::Letter_Titlecase || cat == QChar::Letter_Modifier || cat == QChar::Letter_Other || c == '$' || c == '_'; } \
static bool qtIdentPart(int c) { if (c & 0xffff0000) return false; QChar::Category cat = QChar((unsigned short)c).category(); return cat == QChar::Letter_Uppercase || cat == QChar::Letter_Lowercase || cat == QChar::Letter_Titlecase || cat == QChar::Letter_Modifier || cat == QChar::Letter_Other || cat == QChar::Mark_NonSpacing || cat == QChar::Mark_SpacingCombining || cat == QChar::Number_DecimalDigit || cat == QChar::Punctuation_Connector || c == '$' || c == '_'; } \
static int qtToLower(uint16_t* str, int strLength, uint16_t*& destIfNeeded) { \
destIfNeeded = 0; \
for (int i = 0; i < strLength; ++i) \
str[i] = QChar(str[i]).toLower().unicode(); \
return strLength; } \
static int qtToUpper(uint16_t* str, int strLength, uint16_t*& destIfNeeded) { \
destIfNeeded = 0; \
for (int i = 0; i < strLength; ++i) \
str[i] = QChar(str[i]).toUpper().unicode(); \
return strLength; } \
}
/**
* Set the Qt-based version of the Unicode support functions.
*
* @internal
*/
#define KJS_QT_UNICODE_SET \
{ KJS::UnicodeSupport::setIdentStartChecker(KJS::qtIdentStart); \
KJS::UnicodeSupport::setIdentPartChecker(KJS::qtIdentPart); \
KJS::UnicodeSupport::setToLowerFunction(KJS::qtToLower); \
KJS::UnicodeSupport::setToUpperFunction(KJS::qtToUpper); }
} // namespace
#endif // _KJS_INTERPRETER_H_
|