Bug 190137 - [JSC] Add a C++ callable overload of objectConstructorSeal
Summary: [JSC] Add a C++ callable overload of objectConstructorSeal
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Nobody
URL:
Keywords: InRadar
Depends on:
Blocks: 189947
  Show dependency treegraph
 
Reported: 2018-10-01 06:57 PDT by Koby
Modified: 2018-10-03 05:06 PDT (History)
8 users (show)

See Also:


Attachments
Patch (3.53 KB, patch)
2018-10-01 07:04 PDT, Koby
no flags Details | Formatted Diff | Diff
Patch (3.50 KB, patch)
2018-10-03 04:23 PDT, Koby
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Koby 2018-10-01 06:57:16 PDT
Hi,
In node-jsc, I needed to be able to seal an object from c++. Thus, I've added a objectConstructorSeal overload which receives a JSObject, and refactor the existing objectConstructorSeal to use it (similar to objectConstructorFreeze).

I'm attaching the patch.
Comment 1 Koby 2018-10-01 07:04:55 PDT
Created attachment 351244 [details]
Patch
Comment 2 Yusuke Suzuki 2018-10-01 08:08:55 PDT
Comment on attachment 351244 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=351244&action=review

> Source/JavaScriptCore/runtime/ObjectConstructor.cpp:795
>      if (UNLIKELY(!success)) {
> -        throwTypeError(exec, scope, "Unable to prevent extension in Object.seal"_s);
> -        return encodedJSValue();
> +        return throwTypeError(exec, scope, "Unable to prevent extension in Object.seal"_s);
>      }

Remove `{` and `}`.

> Source/JavaScriptCore/runtime/ObjectConstructor.cpp:803
> +    VM& vm = exec->vm();
> +    auto scope = DECLARE_THROW_SCOPE(vm);

In this case (expression which can cause an error is the last expression, and we do not have the other expressions which can cause an error), we don't need it.

> Source/JavaScriptCore/runtime/ObjectConstructor.cpp:811
> +    JSObject* result = objectConstructorSeal(exec, asObject(obj));
> +    RETURN_IF_EXCEPTION(scope, encodedJSValue());
> +    return JSValue::encode(result);

In this case, we can just do

return JSValue::enccode(objectConstructorSeal(exec, asObject(obj)));
Comment 3 Koby 2018-10-01 10:26:18 PDT
Will upload a new patch. 
Note that your last two comments are also relevant to objectConstructorFreeze which follows (and which I basically copied). Should modify it too and upload a patch (maybe in a separate bug)?
Comment 4 Keith Miller 2018-10-01 12:35:05 PDT
Comment on attachment 351244 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=351244&action=review

>> Source/JavaScriptCore/runtime/ObjectConstructor.cpp:803
>> +    auto scope = DECLARE_THROW_SCOPE(vm);
> 
> In this case (expression which can cause an error is the last expression, and we do not have the other expressions which can cause an error), we don't need it.

I'm personally of the belief that we should have the scope anyway as the code can change in the future. It's good to be safe.

>> Source/JavaScriptCore/runtime/ObjectConstructor.cpp:811
>> +    return JSValue::encode(result);
> 
> In this case, we can just do
> 
> return JSValue::enccode(objectConstructorSeal(exec, asObject(obj)));

If you decide to keep the scope, I would do:

scope.release();
return JSValue::enccode(objectConstructorSeal(exec, asObject(obj)));

Maybe we should have a macro for releasing then returning? We seem to do it a lot.
Comment 5 Koby 2018-10-02 00:39:34 PDT
@Yusuke what do you think? should I remove the scope or leave it and use the new RELEASE_AND_RETURN?
Comment 6 Yusuke Suzuki 2018-10-02 04:46:29 PDT
(In reply to Koby from comment #5)
> @Yusuke what do you think? should I remove the scope or leave it and use the
> new RELEASE_AND_RETURN?

Both are OK to me if you use RETURN_AND_RELEASE (which is already defined in runtime/ExceptionScope.h).
Comment 7 Koby 2018-10-02 06:06:32 PDT
OK, i'll update the code.
Just one more thing that I've noticed and wanted to be sure about: In both objectConstructorSeal and objectConstructorFreeze, if setIntegrityLevel throws an exception an empty value is returned, while if it returns false, an exception is (thrown and) returned. Is this desirable? just making sure.
Comment 8 Yusuke Suzuki 2018-10-02 06:16:12 PDT
(In reply to Koby from comment #7)
> OK, i'll update the code.
> Just one more thing that I've noticed and wanted to be sure about: In both
> objectConstructorSeal and objectConstructorFreeze, if setIntegrityLevel
> throws an exception an empty value is returned, while if it returns false,
> an exception is (thrown and) returned. Is this desirable? just making sure.

Except for some intentional cases (for optimization, see EXCEPTION_ASSERT code in JSC :) ), it is OK that the returned value of the function causing an exception is anything.
Comment 9 Koby 2018-10-03 04:23:33 PDT
Created attachment 351507 [details]
Patch
Comment 10 Yusuke Suzuki 2018-10-03 04:29:10 PDT
Comment on attachment 351507 [details]
Patch

r=me
Comment 11 Koby 2018-10-03 04:30:41 PDT
@Yusuke great, I wasn't sure :)
So just a couple of notes regarding the new patch:
- So now that I've removed the RETURN_IF_EXCEPTION, the javascript callable objectConstructorSeal might return the exception, instead of always returning an empty value (but in the previous comment @Yusuke wrote that it's OK).
- If approved, I would do the same change (RELEASE_AND_RETURN) for objectConstructorFreeze, which is exactly the same. I didn't want to add it to this patch because it's not directly related. What do you t think?
Comment 12 WebKit Commit Bot 2018-10-03 05:05:55 PDT
Comment on attachment 351507 [details]
Patch

Clearing flags on attachment: 351507

Committed r236791: <https://trac.webkit.org/changeset/236791>
Comment 13 WebKit Commit Bot 2018-10-03 05:05:56 PDT
All reviewed patches have been landed.  Closing bug.
Comment 14 Radar WebKit Bug Importer 2018-10-03 05:06:25 PDT
<rdar://problem/44970666>