Commit fc791300 authored by Philip Chimento's avatar Philip Chimento

repo: Factor out new function

The combination of gjs_lookup_generic_prototype() plus
JS_NewObjectWithGivenProto() occurs often enough to be a separate
function.
parent b0b4ec02
...@@ -447,17 +447,10 @@ bool BoxedInstance::get_nested_interface_object( ...@@ -447,17 +447,10 @@ bool BoxedInstance::get_nested_interface_object(
return false; return false;
} }
JS::RootedObject proto(
context, gjs_lookup_generic_prototype(context, interface_info));
if (!proto)
return false;
offset = g_field_info_get_offset (field_info); offset = g_field_info_get_offset (field_info);
JS::RootedObject obj(context, JS_NewObjectWithGivenProto( JS::RootedObject obj(context, gjs_new_object_with_generic_prototype(
context, JS_GetClass(proto), proto)); context, interface_info));
if (!obj) if (!obj)
return false; return false;
...@@ -963,12 +956,7 @@ JSObject* gjs_boxed_from_c_struct(JSContext* cx, GIStructInfo* info, ...@@ -963,12 +956,7 @@ JSObject* gjs_boxed_from_c_struct(JSContext* cx, GIStructInfo* info,
"Wrapping struct %s %p with JSObject", "Wrapping struct %s %p with JSObject",
g_base_info_get_name((GIBaseInfo *)info), gboxed); g_base_info_get_name((GIBaseInfo *)info), gboxed);
JS::RootedObject proto(cx, gjs_lookup_generic_prototype(cx, info)); JS::RootedObject obj(cx, gjs_new_object_with_generic_prototype(cx, info));
if (!proto)
return nullptr;
JS::RootedObject obj(
cx, JS_NewObjectWithGivenProto(cx, JS_GetClass(proto), proto));
if (!obj) if (!obj)
return nullptr; return nullptr;
......
...@@ -382,10 +382,8 @@ gjs_error_from_gerror(JSContext *context, ...@@ -382,10 +382,8 @@ gjs_error_from_gerror(JSContext *context,
"Wrapping struct %s with JSObject", "Wrapping struct %s with JSObject",
g_base_info_get_name((GIBaseInfo *)info)); g_base_info_get_name((GIBaseInfo *)info));
JS::RootedObject proto(context, gjs_lookup_generic_prototype(context, info));
JS::RootedObject obj(context, JS::RootedObject obj(context,
JS_NewObjectWithGivenProto(context, JS_GetClass(proto), proto)); gjs_new_object_with_generic_prototype(context, info));
if (!obj) if (!obj)
return nullptr; return nullptr;
......
...@@ -780,3 +780,12 @@ gjs_lookup_generic_prototype(JSContext *context, ...@@ -780,3 +780,12 @@ gjs_lookup_generic_prototype(JSContext *context,
return &value.toObject(); return &value.toObject();
} }
JSObject* gjs_new_object_with_generic_prototype(JSContext* cx,
GIBaseInfo* info) {
JS::RootedObject proto(cx, gjs_lookup_generic_prototype(cx, info));
if (!proto)
return nullptr;
return JS_NewObjectWithGivenProto(cx, JS_GetClass(proto), proto);
}
...@@ -57,6 +57,9 @@ JSObject * gjs_lookup_generic_constructor (JSContext *context, ...@@ -57,6 +57,9 @@ JSObject * gjs_lookup_generic_constructor (JSContext *context,
GJS_JSAPI_RETURN_CONVENTION GJS_JSAPI_RETURN_CONVENTION
JSObject * gjs_lookup_generic_prototype (JSContext *context, JSObject * gjs_lookup_generic_prototype (JSContext *context,
GIBaseInfo *info); GIBaseInfo *info);
GJS_JSAPI_RETURN_CONVENTION
JSObject* gjs_new_object_with_generic_prototype(JSContext* cx,
GIBaseInfo* info);
GJS_JSAPI_RETURN_CONVENTION GJS_JSAPI_RETURN_CONVENTION
bool gjs_define_info(JSContext *context, bool gjs_define_info(JSContext *context,
......
...@@ -233,13 +233,8 @@ gjs_union_from_c_union(JSContext *context, ...@@ -233,13 +233,8 @@ gjs_union_from_c_union(JSContext *context,
"Wrapping union %s %p with JSObject", "Wrapping union %s %p with JSObject",
g_base_info_get_name((GIBaseInfo *)info), gboxed); g_base_info_get_name((GIBaseInfo *)info), gboxed);
JS::RootedObject proto(context, JS::RootedObject obj(context,
gjs_lookup_generic_prototype(context, (GIUnionInfo*) info)); gjs_new_object_with_generic_prototype(context, info));
if (!proto)
return nullptr;
JS::RootedObject obj(
context, JS_NewObjectWithGivenProto(context, JS_GetClass(proto), proto));
if (!obj) if (!obj)
return nullptr; return nullptr;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment