EncodedJSValue JSC_HOST_CALL objectProtoFuncDefineSetter(ExecState* exec) { JSObject* thisObject = exec->thisValue().toThis(exec, StrictMode).toObject(exec); if (exec->hadException()) return JSValue::encode(jsUndefined()); JSValue set = exec->argument(1); CallData callData; if (getCallData(set, callData) == CallTypeNone) return throwVMError(exec, createTypeError(exec, ASCIILiteral("invalid setter usage"))); auto propertyName = exec->argument(0).toPropertyKey(exec); if (exec->hadException()) return JSValue::encode(jsUndefined()); PropertyDescriptor descriptor; descriptor.setSetter(set); descriptor.setEnumerable(true); descriptor.setConfigurable(true); bool shouldThrow = false; thisObject->methodTable(exec->vm())->defineOwnProperty(thisObject, exec, propertyName, descriptor, shouldThrow); return JSValue::encode(jsUndefined()); }
void ReferencePrototype::finishCreation(VM& vm, JSGlobalObject* globalObject) { Base::finishCreation(vm); this->putDirectNativeFunction(vm, globalObject, vm.propertyNames->toString, 0, referenceProtoFuncToString, NoIntrinsic, DontEnum); PropertyDescriptor descriptor; descriptor.setEnumerable(true); descriptor.setGetter(JSFunction::create(vm, globalObject, 0, WTF::emptyString(), &referenceProtoFuncGetValue)); descriptor.setSetter(JSFunction::create(vm, globalObject, 1, WTF::emptyString(), &referenceProtoFuncSetValue)); Base::defineOwnProperty(this, globalObject->globalExec(), vm.propertyNames->value, descriptor, false); }
EncodedJSValue JSC_HOST_CALL objectProtoFuncDefineSetter(ExecState* exec) { JSObject* thisObject = exec->hostThisValue().toObject(exec); if (exec->hadException()) return JSValue::encode(jsUndefined()); JSValue set = exec->argument(1); CallData callData; if (getCallData(set, callData) == CallTypeNone) return throwVMError(exec, createTypeError(exec, ASCIILiteral("invalid setter usage"))); PropertyDescriptor descriptor; descriptor.setSetter(set); descriptor.setEnumerable(true); descriptor.setConfigurable(true); thisObject->methodTable()->defineOwnProperty(thisObject, exec, Identifier(exec, exec->argument(0).toString(exec)->value(exec)), descriptor, false); return JSValue::encode(jsUndefined()); }
// ES5 8.10.5 ToPropertyDescriptor static bool toPropertyDescriptor(ExecState* exec, JSValue in, PropertyDescriptor& desc) { if (!in.isObject()) { throwError(exec, createTypeError(exec, ASCIILiteral("Property description must be an object."))); return false; } JSObject* description = asObject(in); PropertySlot enumerableSlot(description); if (description->getPropertySlot(exec, exec->propertyNames().enumerable, enumerableSlot)) { desc.setEnumerable(enumerableSlot.getValue(exec, exec->propertyNames().enumerable).toBoolean(exec)); if (exec->hadException()) return false; } PropertySlot configurableSlot(description); if (description->getPropertySlot(exec, exec->propertyNames().configurable, configurableSlot)) { desc.setConfigurable(configurableSlot.getValue(exec, exec->propertyNames().configurable).toBoolean(exec)); if (exec->hadException()) return false; } JSValue value; PropertySlot valueSlot(description); if (description->getPropertySlot(exec, exec->propertyNames().value, valueSlot)) { desc.setValue(valueSlot.getValue(exec, exec->propertyNames().value)); if (exec->hadException()) return false; } PropertySlot writableSlot(description); if (description->getPropertySlot(exec, exec->propertyNames().writable, writableSlot)) { desc.setWritable(writableSlot.getValue(exec, exec->propertyNames().writable).toBoolean(exec)); if (exec->hadException()) return false; } PropertySlot getSlot(description); if (description->getPropertySlot(exec, exec->propertyNames().get, getSlot)) { JSValue get = getSlot.getValue(exec, exec->propertyNames().get); if (exec->hadException()) return false; if (!get.isUndefined()) { CallData callData; if (getCallData(get, callData) == CallTypeNone) { throwError(exec, createTypeError(exec, ASCIILiteral("Getter must be a function."))); return false; } } desc.setGetter(get); } PropertySlot setSlot(description); if (description->getPropertySlot(exec, exec->propertyNames().set, setSlot)) { JSValue set = setSlot.getValue(exec, exec->propertyNames().set); if (exec->hadException()) return false; if (!set.isUndefined()) { CallData callData; if (getCallData(set, callData) == CallTypeNone) { throwError(exec, createTypeError(exec, ASCIILiteral("Setter must be a function."))); return false; } } desc.setSetter(set); } if (!desc.isAccessorDescriptor()) return true; if (desc.value()) { throwError(exec, createTypeError(exec, ASCIILiteral("Invalid property. 'value' present on property with getter or setter."))); return false; } if (desc.writablePresent()) { throwError(exec, createTypeError(exec, ASCIILiteral("Invalid property. 'writable' present on property with getter or setter."))); return false; } return true; }