2 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #import "DOMInternal.h"
34 #import "PlatformString.h"
36 #import "RangeException.h"
37 #import "SVGException.h"
38 #import "WebScriptObjectPrivate.h"
39 #import "XPathEvaluator.h"
40 #import "ScriptController.h"
41 #import "runtime_root.h"
43 //------------------------------------------------------------------------------------------
44 // Wrapping WebCore implementation objects
48 typedef HashMap<DOMObjectInternal*, NSObject*> DOMWrapperMap;
49 static DOMWrapperMap* DOMWrapperCache;
51 NSObject* getDOMWrapper(DOMObjectInternal* impl)
55 return DOMWrapperCache->get(impl);
58 void addDOMWrapper(NSObject* wrapper, DOMObjectInternal* impl)
61 DOMWrapperCache = new DOMWrapperMap;
62 DOMWrapperCache->set(impl, wrapper);
65 void removeDOMWrapper(DOMObjectInternal* impl)
69 DOMWrapperCache->remove(impl);
72 } // namespace WebCore
75 //------------------------------------------------------------------------------------------
77 @implementation WebScriptObject (WebScriptObjectInternal)
79 // Only called by DOMObject subclass.
84 if (![self isKindOfClass:[DOMObject class]]) {
85 [NSException raise:NSGenericException format:@"+%@: _init is an internal initializer", [self class]];
89 _private = [[WebScriptObjectPrivate alloc] init];
90 _private->isCreatedByDOMWrapper = YES;
95 - (void)_initializeScriptDOMNodeImp
97 assert (_private->isCreatedByDOMWrapper);
99 if (![self isKindOfClass:[DOMNode class]]) {
100 // DOMObject can't map back to a document, and thus an interpreter,
101 // so for now only create wrappers for DOMNodes.
102 NSLog(@"%s:%d: We don't know how to create ObjC JS wrappers from DOMObjects yet.", __FILE__, __LINE__);
106 // Extract the WebCore::Node from the ObjectiveC wrapper.
107 DOMNode *n = (DOMNode *)self;
108 WebCore::Node *nodeImpl = [n _node];
110 // Dig up Interpreter and ExecState.
111 WebCore::Frame *frame = 0;
112 if (WebCore::Document* document = nodeImpl->document())
113 frame = document->frame();
117 JSC::ExecState *exec = frame->script()->globalObject()->globalExec();
119 // Get (or create) a cached JS object for the DOM node.
120 JSC::JSObject *scriptImp = asObject(WebCore::toJS(exec, nodeImpl));
122 JSC::Bindings::RootObject* rootObject = frame->script()->bindingRootObject();
124 [self _setImp:scriptImp originRootObject:rootObject rootObject:rootObject];