Skip to content

Commit

Permalink
Added Object.values/Object.entries (#1302)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfwilliams committed May 24, 2022
1 parent 48fde76 commit ae66489
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
Bangle.js2: 6x15 font tweaks for better ISO8859-1 support
Bangle.js: Add clock property to "custom" mode in setUI
Allow method declarations in objects - ES6 'Enhanced Object Literals' (#2202 / #1302)
Added Object.values/Object.entries (#1302)

2v13 : Memory usage improvement: Function scopes no longer stored as an array if they only contain one scope
Memory usage improvement: The root scope is never stored in the scope list (it's searched by default)
Expand Down
52 changes: 52 additions & 0 deletions src/jswrap_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,58 @@ JsVar *jswrap_object_keys_or_property_names(
return arr;
}

/*JSON{
"type" : "staticmethod",
"class" : "Object",
"name" : "values",
"ifndef" : "SAVE_ON_FLASH",
"generate_full" : "jswrap_object_values_or_entries(object, false);",
"params" : [
["object","JsVar","The object to return values for"]
],
"return" : ["JsVar","An array of values - one for each key on the given object"]
}
Return all enumerable values of the given object
*/
/*JSON{
"type" : "staticmethod",
"class" : "Object",
"name" : "entries",
"ifndef" : "SAVE_ON_FLASH",
"generate_full" : "jswrap_object_values_or_entries(object, true);",
"params" : [
["object","JsVar","The object to return values for"]
],
"return" : ["JsVar","An array of `[key,value]` pairs - one for each key on the given object"]
}
Return all enumerable keys and values of the given object
*/
void _jswrap_object_values_cb(void *data, JsVar *name) {
JsVar **cbData = (JsVar**)data;
jsvArrayPushAndUnLock(cbData[0], jspGetVarNamedField(cbData[1], name, false));
}
void _jswrap_object_entries_cb(void *data, JsVar *name) {
JsVar **cbData = (JsVar**)data;
JsVar *tuple = jsvNewEmptyArray();
if (!tuple) return;
jsvArrayPush(tuple, name);
jsvArrayPushAndUnLock(tuple, jspGetVarNamedField(cbData[1], name, false));
jsvArrayPushAndUnLock(cbData[0], tuple);
}
JsVar *jswrap_object_values_or_entries(JsVar *object, bool returnEntries) {
JsVar *cbData[2];
cbData[0] = jsvNewEmptyArray();
cbData[1] = object;
if (!cbData[0]) return 0;
jswrap_object_keys_or_property_names_cb(
object, JSWOKPF_NONE,
returnEntries ? _jswrap_object_entries_cb : _jswrap_object_values_cb,
(void*)cbData
);
return cbData[0];
}


/*JSON{
"type" : "staticmethod",
"class" : "Object",
Expand Down
1 change: 1 addition & 0 deletions src/jswrap_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ void jswrap_object_keys_or_property_names_cb(
JsVar *jswrap_object_keys_or_property_names(
JsVar *obj,
JswObjectKeysOrPropertiesFlags flags);
JsVar *jswrap_object_values_or_entries(JsVar *object, bool returnEntries);
JsVar *jswrap_object_create(JsVar *proto, JsVar *propertiesObject);
JsVar *jswrap_object_getOwnPropertyDescriptor(JsVar *parent, JsVar *name);
bool jswrap_object_hasOwnProperty(JsVar *parent, JsVar *name);
Expand Down

0 comments on commit ae66489

Please sign in to comment.