Bug 197134
Summary: | Can't create a const attribute via the JSC C API | ||
---|---|---|---|
Product: | WebKit | Reporter: | Simon Fraser (smfr) <simon.fraser> |
Component: | JavaScriptCore | Assignee: | Nobody <webkit-unassigned> |
Status: | RESOLVED WORKSFORME | ||
Severity: | Normal | CC: | ggaren, keith_miller, saam, sam, simon.fraser, ysuzuki |
Priority: | P2 | ||
Version: | WebKit Nightly Build | ||
Hardware: | Unspecified | ||
OS: | Unspecified | ||
Bug Depends on: | |||
Bug Blocks: | 197133 |
Simon Fraser (smfr)
There's no way that I could find to create a readonly, const attribute on a class via the JSC C API. I need a
I need the C equivalent of making an attribute with { JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::ConstantInteger, NoIntrinsic }
This is for DumpRenderTree/WebKitTestRunner.
Attachments | ||
---|---|---|
Add attachment proposed patch, testcase, etc. |
Sam Weinig
I think you can do this with using the staticValues array for a class definition:
static JSValueRef GetConstantValue(JSContextRef ctx, JSObjectRef, JSStringRef, JSValueRef*)
{
return JSValueMakeNumber(ctx, 7);
}
JSStaticValue StaticValueArray[] = {
{ "constantValue", GetConstantValue, 0, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
{ 0, 0, 0, 0 }
};
If memory serves me, JSC::PropertyAttribute::ConstantInteger is an optimization, and doesn't change behavior.
Sam Weinig
Or, if you don't want to go the callback route, you can create the properties after the object has been created.
JSObjectSetProperty(ctx, objectToAddPropertyTo, JSStringCreateWithUTF8CString("constantValue), JSValueMakeNumber(ctx, 7), kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, &exceptionPtr);
Saam Barati
(In reply to Sam Weinig from comment #2)
> Or, if you don't want to go the callback route, you can create the
> properties after the object has been created.
>
> JSObjectSetProperty(ctx, objectToAddPropertyTo,
> JSStringCreateWithUTF8CString("constantValue), JSValueMakeNumber(ctx, 7),
> kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete,
> &exceptionPtr);
I think this is what you want.
From the JS spec, these things mean:
- kJSPropertyAttributeReadOnly means you can't Put to the property. E.g, if you have kJSPropertyAttributeReadOnly property "foo", "x.foo = 42" won't succeed. This makes it a "writable: false" property.
- kJSPropertyAttributeDontDelete means the property descriptor is non-configurable. E.g, "delete x.foo" will fail. So will "Object.defineProperty(x, "foo", { ... })"
Simon Fraser (smfr)
OK, that seems to work.