source: webkit/trunk/JavaScriptCore/API/JSContextRef.cpp@ 15133

Last change on this file since 15133 was 15133, checked in by ggaren, 19 years ago

Reviewed by Maciej.


  • Phase 2 in the JS API.


  • Added support for specifying static tables of values -- this should obviate the need for using complicated callbacks for most lookups.


  • API objects are now created with classes (JSClassRef) -- in order to support static values, and in order to prevent API objects from storing their data inline, and thus falling into the oversized (read: slow and prone to giving Maciej the frowny face) heap.


  • Added two specialized JSObject subclasses -- JSCallbackFunction and JSCallbackConstructor -- to allow JSFunctionMake and JSConstructorMake to continue to work with the new class model. Another solution to this problem would be to create a custom class object for each function and constructor you make. This solution is more code but also more efficient.


  • Substantially beefed up the minidom example to demonstrate and test a lot of these techniques. Its output is still pretty haphazard, though.


  • Gave the <kjs/ preface to some includes -- I'm told this matters to building on some versions of Linux.


  • Implemented JSValueIsInstanceOf and JSValueIsObjectOfClass


  • Removed GetDescription callback. Something in the class datastructure should take care of this.
  • API/JSBase.h:
  • API/JSCallbackConstructor.cpp: Added. (KJS::): (KJS::JSCallbackConstructor::JSCallbackConstructor): (KJS::JSCallbackConstructor::implementsConstruct): (KJS::JSCallbackConstructor::construct): (KJS::JSCallbackConstructor::setPrivate): (KJS::JSCallbackConstructor::getPrivate):
  • API/JSCallbackConstructor.h: Added. (KJS::JSCallbackConstructor::classInfo):
  • API/JSCallbackFunction.cpp: Added. (KJS::): (KJS::JSCallbackFunction::JSCallbackFunction): (KJS::JSCallbackFunction::implementsCall): (KJS::JSCallbackFunction::callAsFunction): (KJS::JSCallbackFunction::setPrivate): (KJS::JSCallbackFunction::getPrivate):
  • API/JSCallbackFunction.h: Added. (KJS::JSCallbackFunction::classInfo):
  • API/JSCallbackObject.cpp: (KJS::): (KJS::JSCallbackObject::JSCallbackObject): (KJS::JSCallbackObject::init): (KJS::JSCallbackObject::~JSCallbackObject): (KJS::JSCallbackObject::className): (KJS::JSCallbackObject::getOwnPropertySlot): (KJS::JSCallbackObject::put): (KJS::JSCallbackObject::deleteProperty): (KJS::JSCallbackObject::implementsConstruct): (KJS::JSCallbackObject::construct): (KJS::JSCallbackObject::implementsCall): (KJS::JSCallbackObject::callAsFunction): (KJS::JSCallbackObject::getPropertyList): (KJS::JSCallbackObject::toBoolean): (KJS::JSCallbackObject::toNumber): (KJS::JSCallbackObject::toString): (KJS::JSCallbackObject::inherits): (KJS::JSCallbackObject::staticValueGetter): (KJS::JSCallbackObject::staticFunctionGetter): (KJS::JSCallbackObject::callbackGetter):
  • API/JSCallbackObject.h:
  • API/JSCharBufferRef.cpp:
  • API/JSClassRef.cpp: Added. (JSClassCreate): (JSClassRetain): (JSClassRelease):
  • API/JSClassRef.h: Added. (StaticValueEntry::StaticValueEntry): (StaticFunctionEntry::StaticFunctionEntry): (JSClass::JSClass):
  • API/JSContextRef.cpp: (JSContextCreate): (JSEvaluate):
  • API/JSContextRef.h:
  • API/JSNode.c: Added. (JSNodePrototype_appendChild): (JSNodePrototype_removeChild): (JSNodePrototype_replaceChild): (JSNodePrototype_class): (JSNode_getNodeType): (JSNode_getChildNodes): (JSNode_getFirstChild): (JSNode_finalize): (JSNode_class): (JSNode_prototype): (JSNode_new): (JSNode_construct):
  • API/JSNode.h: Added.
  • API/JSNodeList.c: Added. (JSNodeListPrototype_item): (JSNodeListPrototype_class): (JSNodeList_length): (JSNodeList_getProperty): (JSNodeList_finalize): (JSNodeList_class): (JSNodeList_prototype): (JSNodeList_new):
  • API/JSNodeList.h: Added.
  • API/JSObjectRef.cpp: (JSObjectMake): (JSFunctionMake): (JSConstructorMake): (JSPropertyEnumerator::JSPropertyEnumerator): (JSObjectCreatePropertyEnumerator): (JSPropertyEnumeratorGetNext): (JSPropertyEnumeratorRetain): (JSPropertyEnumeratorRelease):
  • API/JSObjectRef.h: (JSObjectCallbacks::):
  • API/JSValueRef.cpp: (JSValueIsObjectOfClass): (JSValueIsInstanceOf):
  • API/JSValueRef.h:
  • API/Node.c: Added. (Node_new): (Node_appendChild): (Node_removeChild): (Node_replaceChild): (Node_ref): (Node_deref):
  • API/Node.h: Added.
  • API/NodeList.c: Added. (NodeList_new): (NodeList_length): (NodeList_item): (NodeList_ref): (NodeList_deref):
  • API/NodeList.h: Added.
  • API/minidom.c: (main): (print): (createStringWithContentsOfFile):
  • API/minidom.js:
  • API/testapi.c: (assertEqualsAsCharacters): (MyObject_getProperty): (MyObject_class): (myConstructor_callAsConstructor): (main):
  • API/testapi.js:
  • JavaScriptCore.xcodeproj/project.pbxproj:
File size: 4.1 KB
Line 
1// -*- mode: c++; c-basic-offset: 4 -*-
2/*
3 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "APICast.h"
28#include "JSContextRef.h"
29
30#include "JSCallbackObject.h"
31#include "completion.h"
32#include "interpreter.h"
33#include "object.h"
34
35using namespace KJS;
36
37JSContextRef JSContextCreate(JSClassRef globalObjectClass, JSObjectRef globalObjectPrototype)
38{
39 JSLock lock;
40
41 JSObject* jsPrototype = toJS(globalObjectPrototype);
42
43 JSObject* globalObject;
44 if (globalObjectClass) {
45 if (jsPrototype)
46 globalObject = new JSCallbackObject(globalObjectClass, jsPrototype);
47 else
48 globalObject = new JSCallbackObject(globalObjectClass);
49 } else {
50 // creates a slightly more efficient object
51 if (jsPrototype)
52 globalObject = new JSObject(jsPrototype);
53 else
54 globalObject = new JSObject();
55 }
56
57 Interpreter* interpreter = new Interpreter(globalObject); // adds the built-in object prototype to the global object
58 return toRef(interpreter->globalExec());
59}
60
61void JSContextDestroy(JSContextRef context)
62{
63 JSLock lock;
64 ExecState* exec = toJS(context);
65 delete exec->dynamicInterpreter();
66}
67
68JSObjectRef JSContextGetGlobalObject(JSContextRef context)
69{
70 ExecState* exec = toJS(context);
71 return toRef(exec->dynamicInterpreter()->globalObject());
72}
73
74bool JSEvaluate(JSContextRef context, JSValueRef thisValue, JSCharBufferRef script, JSCharBufferRef sourceURL, int startingLineNumber, JSValueRef* returnValue)
75{
76 JSLock lock;
77 ExecState* exec = toJS(context);
78 JSValue* jsThisValue = toJS(thisValue);
79 UString::Rep* scriptRep = toJS(script);
80 UString::Rep* sourceURLRep = toJS(sourceURL);
81 Completion completion = exec->dynamicInterpreter()->evaluate(UString(sourceURLRep), startingLineNumber, UString(scriptRep), jsThisValue);
82
83 if (returnValue)
84 *returnValue = completion.value() ? toRef(completion.value()) : toRef(jsUndefined());
85
86 return completion.complType() != Throw;
87}
88
89bool JSCheckSyntax(JSContextRef context, JSCharBufferRef script)
90{
91 JSLock lock;
92 ExecState* exec = toJS(context);
93 UString::Rep* rep = toJS(script);
94 return exec->dynamicInterpreter()->checkSyntax(UString(rep));
95}
96
97bool JSContextHasException(JSContextRef context)
98{
99 ExecState* exec = toJS(context);
100 return exec->hadException();
101}
102
103JSValueRef JSContextGetException(JSContextRef context)
104{
105 ExecState* exec = toJS(context);
106 return toRef(exec->exception());
107}
108
109void JSContextClearException(JSContextRef context)
110{
111 ExecState* exec = toJS(context);
112 if (exec->hadException())
113 exec->clearException();
114}
115
116void JSContextSetException(JSContextRef context, JSValueRef value)
117{
118 ExecState* exec = toJS(context);
119 JSValue* jsValue = toJS(value);
120 exec->setException(jsValue);
121}
122
Note: See TracBrowser for help on using the repository browser.