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.
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.
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);
(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", { ... })"
OK, that seems to work.