Skip to content

Commit

Permalink
add length property for function objects
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderBrevig committed Jan 16, 2015
1 parent ec52131 commit 13bd162
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
21 changes: 21 additions & 0 deletions src/jsvar.c
Expand Up @@ -1497,6 +1497,27 @@ JsVar *jsvArrayBufferGetFromName(JsVar *name) {
return value;
}


JsVar *jsvGetFunctionArgumentLength(JsVar *functionScope) {
JsVar *args = jsvNewWithFlags(JSV_ARRAY);
if (!args) return 0; // out of memory

JsvObjectIterator it;
jsvObjectIteratorNew(&it, functionScope);
while (jsvObjectIteratorHasValue(&it)) {
JsVar *idx = jsvObjectIteratorGetKey(&it);
if (jsvIsFunctionParameter(idx)) {
JsVar *val = jsvSkipOneName(idx);
jsvArrayPushAndUnLock(args, val);
}
jsvUnLock(idx);
jsvObjectIteratorNext(&it);
}
jsvObjectIteratorFree(&it);

return args;
}

/** If a is a name skip it and go to what it points to - and so on.
* ALWAYS locks - so must unlock what it returns. It MAY
* return 0. */
Expand Down
5 changes: 5 additions & 0 deletions src/jsvar.h
Expand Up @@ -435,6 +435,8 @@ bool jsvGetBoolAndUnLock(JsVar *v);
long long jsvGetLongIntegerAndUnLock(JsVar *v);




/** Get the item at the given location in the array buffer and return the result */
size_t jsvGetArrayBufferLength(JsVar *arrayBuffer);
/** Get the String the contains the data for this arrayBuffer */
Expand All @@ -446,6 +448,9 @@ void jsvArrayBufferSet(JsVar *arrayBuffer, size_t index, JsVar *value);
/** Given an integer name that points to an arraybuffer or an arraybufferview, evaluate it and return the result */
JsVar *jsvArrayBufferGetFromName(JsVar *name);

/** Get the number of arguments for a given function */
JsVar *jsvGetFunctionArgumentLength(JsVar *scope);

/** If a is a name skip it and go to what it points to - and so on.
* ALWAYS locks - so must unlock what it returns. It MAY
* return 0. */
Expand Down
19 changes: 2 additions & 17 deletions src/jswrap_functions.c
Expand Up @@ -37,26 +37,11 @@ JsVar *jswrap_arguments() {
return 0;
}

JsVar *args = jsvNewWithFlags(JSV_ARRAY);
if (!args) return 0; // out of memory

JsvObjectIterator it;
jsvObjectIteratorNew(&it, scope);
while (jsvObjectIteratorHasValue(&it)) {
JsVar *idx = jsvObjectIteratorGetKey(&it);
if (jsvIsFunctionParameter(idx)) {
JsVar *val = jsvSkipOneName(idx);
jsvArrayPushAndUnLock(args, val);
}
jsvUnLock(idx);
jsvObjectIteratorNext(&it);
}
jsvObjectIteratorFree(&it);

return args;
return jsvGetFunctionArgumentLength(scope);
}



/*JSON{
"type" : "constructor",
"class" : "Function",
Expand Down
4 changes: 3 additions & 1 deletion src/jswrap_object.c
Expand Up @@ -57,9 +57,11 @@ JsVar *jswrap_object_length(JsVar *parent) {
if (jsvIsArray(parent)) {
return jsvNewFromInteger(jsvGetArrayLength(parent));
} else if (jsvIsArrayBuffer(parent)) {
return jsvNewFromInteger((JsVarInt)jsvGetArrayBufferLength(parent));
return jsvNewFromInteger((JsVarInt)jsvGetArrayBufferLength(parent));
} else if (jsvIsString(parent)) {
return jsvNewFromInteger((JsVarInt)jsvGetStringLength(parent));
} else if (jsvIsFunction(parent)) {
return jswrap_object_length(jsvGetFunctionArgumentLength(parent));
}
return 0;
}
Expand Down

0 comments on commit 13bd162

Please sign in to comment.