DOMInternal.mm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (C) 2004, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  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.
  12. *
  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.
  24. */
  25. #import "config.h"
  26. #import "DOMInternal.h"
  27. #import "DOMNodeInternal.h"
  28. #import "Frame.h"
  29. #import "JSNode.h"
  30. #import "ScriptController.h"
  31. #import "WebScriptObjectPrivate.h"
  32. #import "runtime_root.h"
  33. //------------------------------------------------------------------------------------------
  34. // Wrapping WebCore implementation objects
  35. static NSMapTable* DOMWrapperCache;
  36. #if COMPILER(CLANG)
  37. #pragma clang diagnostic push
  38. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  39. #endif
  40. NSMapTable* createWrapperCache()
  41. {
  42. // NSMapTable with zeroing weak pointers is the recommended way to build caches like this under garbage collection.
  43. NSPointerFunctionsOptions keyOptions = NSPointerFunctionsOpaqueMemory | NSPointerFunctionsOpaquePersonality;
  44. NSPointerFunctionsOptions valueOptions = NSPointerFunctionsZeroingWeakMemory | NSPointerFunctionsObjectPersonality;
  45. return [[NSMapTable alloc] initWithKeyOptions:keyOptions valueOptions:valueOptions capacity:0];
  46. }
  47. #if COMPILER(CLANG)
  48. #pragma clang diagnostic pop
  49. #endif
  50. NSObject* getDOMWrapper(DOMObjectInternal* impl)
  51. {
  52. if (!DOMWrapperCache)
  53. return nil;
  54. return static_cast<NSObject*>(NSMapGet(DOMWrapperCache, impl));
  55. }
  56. void addDOMWrapper(NSObject* wrapper, DOMObjectInternal* impl)
  57. {
  58. if (!DOMWrapperCache)
  59. DOMWrapperCache = createWrapperCache();
  60. NSMapInsert(DOMWrapperCache, impl, wrapper);
  61. }
  62. void removeDOMWrapper(DOMObjectInternal* impl)
  63. {
  64. if (!DOMWrapperCache)
  65. return;
  66. NSMapRemove(DOMWrapperCache, impl);
  67. }
  68. //------------------------------------------------------------------------------------------
  69. @implementation WebScriptObject (WebScriptObjectInternal)
  70. // Only called by DOMObject subclass.
  71. - (id)_init
  72. {
  73. self = [super init];
  74. if (![self isKindOfClass:[DOMObject class]]) {
  75. [NSException raise:NSGenericException format:@"+%@: _init is an internal initializer", [self class]];
  76. return nil;
  77. }
  78. _private = [[WebScriptObjectPrivate alloc] init];
  79. _private->isCreatedByDOMWrapper = YES;
  80. return self;
  81. }
  82. - (void)_initializeScriptDOMNodeImp
  83. {
  84. ASSERT(_private->isCreatedByDOMWrapper);
  85. if (![self isKindOfClass:[DOMNode class]]) {
  86. // DOMObject can't map back to a document, and thus an interpreter,
  87. // so for now only create wrappers for DOMNodes.
  88. NSLog(@"%s:%d: We don't know how to create ObjC JS wrappers from DOMObjects yet.", __FILE__, __LINE__);
  89. return;
  90. }
  91. // Extract the WebCore::Node from the ObjectiveC wrapper.
  92. DOMNode *n = (DOMNode *)self;
  93. WebCore::Node *nodeImpl = core(n);
  94. // Dig up Interpreter and ExecState.
  95. WebCore::Frame *frame = 0;
  96. if (WebCore::Document* document = nodeImpl->document())
  97. frame = document->frame();
  98. if (!frame)
  99. return;
  100. // The global object which should own this node - FIXME: does this need to be isolated-world aware?
  101. WebCore::JSDOMGlobalObject* globalObject = frame->script()->globalObject(WebCore::mainThreadNormalWorld());
  102. JSC::ExecState *exec = globalObject->globalExec();
  103. // Get (or create) a cached JS object for the DOM node.
  104. JSC::JSObject *scriptImp = asObject(WebCore::toJS(exec, globalObject, nodeImpl));
  105. JSC::Bindings::RootObject* rootObject = frame->script()->bindingRootObject();
  106. [self _setImp:scriptImp originRootObject:rootObject rootObject:rootObject];
  107. }
  108. @end