JSContextRef.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright (C) 2006, 2007 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. #include "config.h"
  26. #include "JSContextRef.h"
  27. #include "JSContextRefPrivate.h"
  28. #include "APICast.h"
  29. #include "InitializeThreading.h"
  30. #include <interpreter/CallFrame.h>
  31. #include <interpreter/Interpreter.h>
  32. #include "JSCallbackObject.h"
  33. #include "JSClassRef.h"
  34. #include "JSGlobalObject.h"
  35. #include "JSObject.h"
  36. #include "Operations.h"
  37. #include "SourceProvider.h"
  38. #include <wtf/text/StringBuilder.h>
  39. #include <wtf/text/StringHash.h>
  40. #if OS(DARWIN)
  41. #include <mach-o/dyld.h>
  42. static const int32_t webkitFirstVersionWithConcurrentGlobalContexts = 0x2100500; // 528.5.0
  43. #endif
  44. using namespace JSC;
  45. // From the API's perspective, a context group remains alive iff
  46. // (a) it has been JSContextGroupRetained
  47. // OR
  48. // (b) one of its contexts has been JSContextRetained
  49. JSContextGroupRef JSContextGroupCreate()
  50. {
  51. initializeThreading();
  52. return toRef(VM::createContextGroup().leakRef());
  53. }
  54. JSContextGroupRef JSContextGroupRetain(JSContextGroupRef group)
  55. {
  56. toJS(group)->ref();
  57. return group;
  58. }
  59. void JSContextGroupRelease(JSContextGroupRef group)
  60. {
  61. IdentifierTable* savedIdentifierTable;
  62. VM& vm = *toJS(group);
  63. {
  64. JSLockHolder lock(vm);
  65. savedIdentifierTable = wtfThreadData().setCurrentIdentifierTable(vm.identifierTable);
  66. vm.deref();
  67. }
  68. wtfThreadData().setCurrentIdentifierTable(savedIdentifierTable);
  69. }
  70. static bool internalScriptTimeoutCallback(ExecState* exec, void* callbackPtr, void* callbackData)
  71. {
  72. JSShouldTerminateCallback callback = reinterpret_cast<JSShouldTerminateCallback>(callbackPtr);
  73. JSContextRef contextRef = toRef(exec);
  74. ASSERT(callback);
  75. return callback(contextRef, callbackData);
  76. }
  77. void JSContextGroupSetExecutionTimeLimit(JSContextGroupRef group, double limit, JSShouldTerminateCallback callback, void* callbackData)
  78. {
  79. VM& vm = *toJS(group);
  80. APIEntryShim entryShim(&vm);
  81. Watchdog& watchdog = vm.watchdog;
  82. if (callback) {
  83. void* callbackPtr = reinterpret_cast<void*>(callback);
  84. watchdog.setTimeLimit(vm, limit, internalScriptTimeoutCallback, callbackPtr, callbackData);
  85. } else
  86. watchdog.setTimeLimit(vm, limit);
  87. }
  88. void JSContextGroupClearExecutionTimeLimit(JSContextGroupRef group)
  89. {
  90. VM& vm = *toJS(group);
  91. APIEntryShim entryShim(&vm);
  92. Watchdog& watchdog = vm.watchdog;
  93. watchdog.setTimeLimit(vm, std::numeric_limits<double>::infinity());
  94. }
  95. // From the API's perspective, a global context remains alive iff it has been JSGlobalContextRetained.
  96. JSGlobalContextRef JSGlobalContextCreate(JSClassRef globalObjectClass)
  97. {
  98. initializeThreading();
  99. #if OS(DARWIN)
  100. // If the application was linked before JSGlobalContextCreate was changed to use a unique VM,
  101. // we use a shared one for backwards compatibility.
  102. if (NSVersionOfLinkTimeLibrary("JavaScriptCore") <= webkitFirstVersionWithConcurrentGlobalContexts) {
  103. return JSGlobalContextCreateInGroup(toRef(&VM::sharedInstance()), globalObjectClass);
  104. }
  105. #endif // OS(DARWIN)
  106. return JSGlobalContextCreateInGroup(0, globalObjectClass);
  107. }
  108. JSGlobalContextRef JSGlobalContextCreateInGroup(JSContextGroupRef group, JSClassRef globalObjectClass)
  109. {
  110. initializeThreading();
  111. RefPtr<VM> vm = group ? PassRefPtr<VM>(toJS(group)) : VM::createContextGroup();
  112. APIEntryShim entryShim(vm.get(), false);
  113. vm->makeUsableFromMultipleThreads();
  114. if (!globalObjectClass) {
  115. JSGlobalObject* globalObject = JSGlobalObject::create(*vm, JSGlobalObject::createStructure(*vm, jsNull()));
  116. return JSGlobalContextRetain(toGlobalRef(globalObject->globalExec()));
  117. }
  118. JSGlobalObject* globalObject = JSCallbackObject<JSGlobalObject>::create(*vm, globalObjectClass, JSCallbackObject<JSGlobalObject>::createStructure(*vm, 0, jsNull()));
  119. ExecState* exec = globalObject->globalExec();
  120. JSValue prototype = globalObjectClass->prototype(exec);
  121. if (!prototype)
  122. prototype = jsNull();
  123. globalObject->resetPrototype(*vm, prototype);
  124. return JSGlobalContextRetain(toGlobalRef(exec));
  125. }
  126. JSGlobalContextRef JSGlobalContextRetain(JSGlobalContextRef ctx)
  127. {
  128. ExecState* exec = toJS(ctx);
  129. APIEntryShim entryShim(exec);
  130. VM& vm = exec->vm();
  131. gcProtect(exec->dynamicGlobalObject());
  132. vm.ref();
  133. return ctx;
  134. }
  135. void JSGlobalContextRelease(JSGlobalContextRef ctx)
  136. {
  137. IdentifierTable* savedIdentifierTable;
  138. ExecState* exec = toJS(ctx);
  139. {
  140. JSLockHolder lock(exec);
  141. VM& vm = exec->vm();
  142. savedIdentifierTable = wtfThreadData().setCurrentIdentifierTable(vm.identifierTable);
  143. bool protectCountIsZero = Heap::heap(exec->dynamicGlobalObject())->unprotect(exec->dynamicGlobalObject());
  144. if (protectCountIsZero)
  145. vm.heap.reportAbandonedObjectGraph();
  146. vm.deref();
  147. }
  148. wtfThreadData().setCurrentIdentifierTable(savedIdentifierTable);
  149. }
  150. JSObjectRef JSContextGetGlobalObject(JSContextRef ctx)
  151. {
  152. if (!ctx) {
  153. ASSERT_NOT_REACHED();
  154. return 0;
  155. }
  156. ExecState* exec = toJS(ctx);
  157. APIEntryShim entryShim(exec);
  158. // It is necessary to call toThisObject to get the wrapper object when used with WebCore.
  159. return toRef(exec->lexicalGlobalObject()->methodTable()->toThisObject(exec->lexicalGlobalObject(), exec));
  160. }
  161. JSContextGroupRef JSContextGetGroup(JSContextRef ctx)
  162. {
  163. if (!ctx) {
  164. ASSERT_NOT_REACHED();
  165. return 0;
  166. }
  167. ExecState* exec = toJS(ctx);
  168. return toRef(&exec->vm());
  169. }
  170. JSGlobalContextRef JSContextGetGlobalContext(JSContextRef ctx)
  171. {
  172. if (!ctx) {
  173. ASSERT_NOT_REACHED();
  174. return 0;
  175. }
  176. ExecState* exec = toJS(ctx);
  177. APIEntryShim entryShim(exec);
  178. return toGlobalRef(exec->lexicalGlobalObject()->globalExec());
  179. }
  180. JSStringRef JSContextCreateBacktrace(JSContextRef ctx, unsigned maxStackSize)
  181. {
  182. if (!ctx) {
  183. ASSERT_NOT_REACHED();
  184. return 0;
  185. }
  186. ExecState* exec = toJS(ctx);
  187. JSLockHolder lock(exec);
  188. StringBuilder builder;
  189. Vector<StackFrame> stackTrace;
  190. Interpreter::getStackTrace(&exec->vm(), stackTrace, maxStackSize);
  191. for (size_t i = 0; i < stackTrace.size(); i++) {
  192. String urlString;
  193. String functionName;
  194. StackFrame& frame = stackTrace[i];
  195. JSValue function = frame.callee.get();
  196. if (frame.callee)
  197. functionName = frame.friendlyFunctionName(exec);
  198. else {
  199. // Caller is unknown, but if frame is empty we should still add the frame, because
  200. // something called us, and gave us arguments.
  201. if (i)
  202. break;
  203. }
  204. unsigned lineNumber;
  205. unsigned column;
  206. frame.computeLineAndColumn(lineNumber, column);
  207. if (!builder.isEmpty())
  208. builder.append('\n');
  209. builder.append('#');
  210. builder.appendNumber(i);
  211. builder.append(' ');
  212. builder.append(functionName);
  213. builder.appendLiteral("() at ");
  214. builder.append(urlString);
  215. if (frame.codeType != StackFrameNativeCode) {
  216. builder.append(':');
  217. builder.appendNumber(lineNumber);
  218. }
  219. if (!function)
  220. break;
  221. }
  222. return OpaqueJSString::create(builder.toString()).leakRef();
  223. }