@@ -299,20 +299,28 @@ Ref<JSValue> scArrayRemove(FunctionScope* pScope) {
299
299
v = v->nextSibling;
300
300
}
301
301
}
302
+ */
302
303
303
- Ref<JSValue>scArrayJoin(FunctionScope* pScope) {
304
- string sep = pScope->getParam("separator")->toString();
305
- CScriptVar *arr = pScope->getThis();
304
+ Ref<JSValue>scArrayJoin (FunctionScope* pScope)
305
+ {
306
+ Ref<JSArray> arr = pScope->getThis ().staticCast <JSArray>();
307
+ auto sep = pScope->getParam (" separator" );
308
+ string sepStr = " ," ;
306
309
307
- ostringstream sstr;
308
- int l = arr->getArrayLength();
309
- for (int i=0;i<l;i++) {
310
- if (i>0) sstr << sep;
311
- sstr << arr->getArrayIndex(i)->toString();
312
- }
310
+ if (!sep->isNull ())
311
+ sepStr = sep->toString ();
313
312
314
- return jsString(sstr.str());
315
- }*/
313
+ ostringstream output;
314
+ const size_t n = arr->length ();
315
+ for (size_t i = 0 ; i < n; i++)
316
+ {
317
+ if (i > 0 )
318
+ output << sepStr;
319
+ output << arr->getAt (i)->toString ();
320
+ }
321
+
322
+ return jsString (output.str ());
323
+ }
316
324
317
325
// ----------------------------------------------- Register Functions
318
326
@@ -377,7 +385,7 @@ void registerFunctions(Ref<IScope> scope)
377
385
// JSON.parse is left out as you can (unsafely!) use eval instead
378
386
// addNative("function Array.contains(obj)", scArrayContains, scope);
379
387
// addNative("function Array.remove(obj)", scArrayRemove, scope);
380
- // addNative("function Array.join(separator)", scArrayJoin, scope);
388
+ addNative (" function Array.prototype .join(separator)" , scArrayJoin, scope);
381
389
addNative (" function Array.prototype.push(x)" , scArrayPush, scope);
382
390
addNative (" function Array.prototype.indexOf(searchElement, fromIndex)" , scArrayIndexOf, scope);
383
391
}
0 commit comments