EncodedJSValue JSC_HOST_CALL constructJSWorker(ExecState& exec) { VM& vm = exec.vm(); auto scope = DECLARE_THROW_SCOPE(vm); DOMConstructorObject* jsConstructor = jsCast<DOMConstructorObject*>(exec.callee()); if (!exec.argumentCount()) return throwVMError(&exec, scope, createNotEnoughArgumentsError(&exec)); String scriptURL = exec.uncheckedArgument(0).toWTFString(&exec); if (exec.hadException()) return JSValue::encode(JSValue()); // See section 4.8.2 step 14 of WebWorkers for why this is the lexicalGlobalObject. DOMWindow& window = asJSDOMWindow(exec.lexicalGlobalObject())->wrapped(); ExceptionCode ec = 0; ASSERT(window.document()); RefPtr<Worker> worker = Worker::create(*window.document(), scriptURL, ec); if (ec) { setDOMException(&exec, ec); return JSValue::encode(JSValue()); } return JSValue::encode(toJSNewlyCreated(&exec, jsConstructor->globalObject(), WTFMove(worker))); }
JSValue JSDocument::createTouchList(ExecState& state) { auto touchList = TouchList::create(); for (size_t i = 0; i < state.argumentCount(); ++i) { auto* item = JSTouch::toWrapped(state.uncheckedArgument(i)); if (!item) return JSValue::decode(throwArgumentTypeError(state, i, "touches", "Document", "createTouchList", "Touch")); touchList->append(*item); } return toJSNewlyCreated(&state, globalObject(), WTFMove(touchList)); }
JSValue JSXMLHttpRequest::retrieveResponse(ExecState& state) { auto type = wrapped().responseType(); switch (type) { case XMLHttpRequest::ResponseType::EmptyString: case XMLHttpRequest::ResponseType::Text: return responseText(state); default: break; } if (!wrapped().doneWithoutErrors()) return jsNull(); JSValue value; switch (type) { case XMLHttpRequest::ResponseType::EmptyString: case XMLHttpRequest::ResponseType::Text: ASSERT_NOT_REACHED(); return jsUndefined(); case XMLHttpRequest::ResponseType::Json: value = JSONParse(&state, wrapped().responseTextIgnoringResponseType()); if (!value) value = jsNull(); break; case XMLHttpRequest::ResponseType::Document: { ExceptionCode ec = 0; auto document = wrapped().responseXML(ec); ASSERT(!ec); value = toJS(&state, globalObject(), document); break; } case XMLHttpRequest::ResponseType::Blob: value = toJSNewlyCreated(&state, globalObject(), wrapped().createResponseBlob()); break; case XMLHttpRequest::ResponseType::Arraybuffer: value = toJS(&state, globalObject(), wrapped().createResponseArrayBuffer()); break; } wrapped().didCacheResponse(); return value; }
EncodedJSValue JSC_HOST_CALL constructJSMutationObserver(ExecState& exec) { if (exec.argumentCount() < 1) return throwVMError(&exec, createNotEnoughArgumentsError(&exec)); JSObject* object = exec.uncheckedArgument(0).getObject(); CallData callData; if (!object || object->methodTable()->getCallData(object, callData) == CallType::None) return throwVMTypeError(&exec, ASCIILiteral("Callback argument must be a function")); DOMConstructorObject* jsConstructor = jsCast<DOMConstructorObject*>(exec.callee()); auto callback = JSMutationCallback::create(object, jsConstructor->globalObject()); JSObject* jsObserver = asObject(toJSNewlyCreated(&exec, jsConstructor->globalObject(), MutationObserver::create(WTFMove(callback)))); PrivateName propertyName; jsObserver->putDirect(jsConstructor->globalObject()->vm(), propertyName, object); return JSValue::encode(jsObserver); }
EncodedJSValue JSC_HOST_CALL constructJSMutationObserver(ExecState& exec) { VM& vm = exec.vm(); auto scope = DECLARE_THROW_SCOPE(vm); if (exec.argumentCount() < 1) return throwVMError(&exec, scope, createNotEnoughArgumentsError(&exec)); JSObject* object = exec.uncheckedArgument(0).getObject(); CallData callData; if (!object || object->methodTable()->getCallData(object, callData) == CallType::None) return throwArgumentTypeError(exec, scope, 0, "callback", "MutationObserver", nullptr, "MutationCallback"); DOMConstructorObject* jsConstructor = jsCast<DOMConstructorObject*>(exec.jsCallee()); auto callback = JSMutationCallback::create(object, jsConstructor->globalObject()); JSObject* jsObserver = asObject(toJSNewlyCreated(&exec, jsConstructor->globalObject(), MutationObserver::create(WTFMove(callback)))); PrivateName propertyName; jsObserver->putDirect(vm, propertyName, object); return JSValue::encode(jsObserver); }
EncodedJSValue JSC_HOST_CALL constructJSWorker(ExecState& state) { VM& vm = state.vm(); auto scope = DECLARE_THROW_SCOPE(vm); ASSERT(jsCast<DOMConstructorObject*>(state.jsCallee())); ASSERT(jsCast<DOMConstructorObject*>(state.jsCallee())->globalObject()); auto& globalObject = *jsCast<DOMConstructorObject*>(state.jsCallee())->globalObject(); if (!state.argumentCount()) return throwVMError(&state, scope, createNotEnoughArgumentsError(&state)); String scriptURL = state.uncheckedArgument(0).toWTFString(&state); RETURN_IF_EXCEPTION(scope, encodedJSValue()); // See section 4.8.2 step 14 of WebWorkers for why this is the lexicalGlobalObject. auto& window = asJSDOMWindow(state.lexicalGlobalObject())->wrapped(); ASSERT(window.document()); return JSValue::encode(toJSNewlyCreated(state, globalObject, scope, Worker::create(*window.document(), scriptURL, globalObject.runtimeFlags()))); }
template<> EncodedJSValue JSC_HOST_CALL JSTestInterfaceConstructor::construct(ExecState* state) { auto* castedThis = jsCast<JSTestInterfaceConstructor*>(state->callee()); if (UNLIKELY(state->argumentCount() < 1)) return throwVMError(state, createNotEnoughArgumentsError(state)); ExceptionCode ec = 0; auto str1 = state->argument(0).toWTFString(state); if (UNLIKELY(state->hadException())) return JSValue::encode(jsUndefined()); auto str2 = state->argument(1).isUndefined() ? ASCIILiteral("defaultString") : state->uncheckedArgument(1).toWTFString(state); if (UNLIKELY(state->hadException())) return JSValue::encode(jsUndefined()); ScriptExecutionContext* context = castedThis->scriptExecutionContext(); if (UNLIKELY(!context)) return throwConstructorDocumentUnavailableError(*state, "TestInterface"); auto object = TestInterface::create(*context, str1, str2, ec); if (UNLIKELY(ec)) { setDOMException(state, ec); return JSValue::encode(JSValue()); } return JSValue::encode(asObject(toJSNewlyCreated(state, castedThis->globalObject(), WTFMove(object)))); }
JSValue toJS(ExecState* state, JSDOMGlobalObject* globalObject, Document& document) { if (auto* wrapper = cachedDocumentWrapper(*state, *globalObject, document)) return wrapper; return toJSNewlyCreated(state, globalObject, Ref<Document>(document)); }