MCPcopy Create free account
hub / github.com/buke/quickjs-go / BindAccessorToObject

Function BindAccessorToObject

bridge.c:720–761  ·  view source on GitHub ↗

BindAccessorToObject - Bind an accessor to a JavaScript object

Source from the content-addressed store, hash-verified

718
719// BindAccessorToObject - Bind an accessor to a JavaScript object
720JSValue BindAccessorToObject(JSContext *ctx, JSValue obj, const AccessorEntry *accessor) {
721 JSAtom accessor_atom = JS_NewAtom(ctx, accessor->name);
722 JSValue getter = JS_UNDEFINED;
723 JSValue setter = JS_UNDEFINED;
724
725 // Create getter
726 if (accessor->getter_id != 0) {
727 getter = CreateCFunction(ctx, accessor->name, 0,
728 JS_CFUNC_getter_magic, accessor->getter_id);
729 if (JS_IsException(getter)) {
730 JS_FreeAtom(ctx, accessor_atom);
731 return getter;
732 }
733 }
734
735 // Create setter
736 if (accessor->setter_id != 0) {
737 setter = CreateCFunction(ctx, accessor->name, 1,
738 JS_CFUNC_setter_magic, accessor->setter_id);
739 if (JS_IsException(setter)) {
740 JS_FreeAtom(ctx, accessor_atom);
741 if (!JS_IsUndefined(getter)) {
742 JS_FreeValue(ctx, getter);
743 }
744 return setter;
745 }
746 }
747
748 // Define accessor
749 int result = JS_DefinePropertyGetSet(ctx, obj, accessor_atom, getter, setter,
750 JS_PROP_CONFIGURABLE);
751
752 JS_FreeAtom(ctx, accessor_atom);
753
754 if (result < 0) {
755 if (!JS_IsUndefined(getter)) JS_FreeValue(ctx, getter);
756 if (!JS_IsUndefined(setter)) JS_FreeValue(ctx, setter);
757 return JS_ThrowInternalError(ctx, "failed to bind accessor: %s", accessor->name);
758 }
759
760 return JS_UNDEFINED;
761}
762
763// BindPropertyToObject - Bind a data property to a JavaScript object
764// This creates real data properties using JS_DefinePropertyValueStr

Callers 1

BindMembersToObjectFunction · 0.85

Calls 3

CreateCFunctionFunction · 0.85
JS_IsExceptionFunction · 0.85
JS_IsUndefinedFunction · 0.85

Tested by

no test coverage detected