<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>197134</bug_id>
          
          <creation_ts>2019-04-19 21:01:09 -0700</creation_ts>
          <short_desc>Can&apos;t create a const attribute via the JSC C API</short_desc>
          <delta_ts>2019-04-22 15:39:13 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>JavaScriptCore</component>
          <version>WebKit Nightly Build</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>WORKSFORME</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          <blocked>197133</blocked>
          <everconfirmed>1</everconfirmed>
          <reporter name="Simon Fraser (smfr)">simon.fraser</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>ggaren</cc>
    
    <cc>keith_miller</cc>
    
    <cc>saam</cc>
    
    <cc>sam</cc>
    
    <cc>simon.fraser</cc>
    
    <cc>ysuzuki</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1529030</commentid>
    <comment_count>0</comment_count>
    <who name="Simon Fraser (smfr)">simon.fraser</who>
    <bug_when>2019-04-19 21:01:09 -0700</bug_when>
    <thetext>There&apos;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.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1529122</commentid>
    <comment_count>1</comment_count>
    <who name="Sam Weinig">sam</who>
    <bug_when>2019-04-20 19:39:52 -0700</bug_when>
    <thetext>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[] = {
    { &quot;constantValue&quot;, GetConstantValue, 0, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
    { 0, 0, 0, 0 }
};

If memory serves me, JSC::PropertyAttribute::ConstantInteger is an optimization, and doesn&apos;t change behavior.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1529123</commentid>
    <comment_count>2</comment_count>
    <who name="Sam Weinig">sam</who>
    <bug_when>2019-04-20 19:44:37 -0700</bug_when>
    <thetext>Or, if you don&apos;t want to go the callback route, you can create the properties after the object has been created.

JSObjectSetProperty(ctx, objectToAddPropertyTo, JSStringCreateWithUTF8CString(&quot;constantValue), JSValueMakeNumber(ctx, 7), kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, &amp;exceptionPtr);</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1529299</commentid>
    <comment_count>3</comment_count>
    <who name="Saam Barati">saam</who>
    <bug_when>2019-04-22 10:49:35 -0700</bug_when>
    <thetext>(In reply to Sam Weinig from comment #2)
&gt; Or, if you don&apos;t want to go the callback route, you can create the
&gt; properties after the object has been created.
&gt; 
&gt; JSObjectSetProperty(ctx, objectToAddPropertyTo,
&gt; JSStringCreateWithUTF8CString(&quot;constantValue), JSValueMakeNumber(ctx, 7),
&gt; kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete,
&gt; &amp;exceptionPtr);

I think this is what you want.

From the JS spec, these things mean:
- kJSPropertyAttributeReadOnly means you can&apos;t Put to the property. E.g, if you have kJSPropertyAttributeReadOnly property &quot;foo&quot;, &quot;x.foo = 42&quot; won&apos;t succeed. This makes it a &quot;writable: false&quot; property.
- kJSPropertyAttributeDontDelete means the property descriptor is non-configurable. E.g, &quot;delete x.foo&quot; will fail. So will &quot;Object.defineProperty(x, &quot;foo&quot;, { ... })&quot;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1529438</commentid>
    <comment_count>4</comment_count>
    <who name="Simon Fraser (smfr)">simon.fraser</who>
    <bug_when>2019-04-22 15:39:13 -0700</bug_when>
    <thetext>OK, that seems to work.</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>