MCPcopy Create free account
hub / github.com/Constellation/iv / ArrayToLocaleString

Function ArrayToLocaleString

iv/lv5/runtime/array.cc:165–222  ·  view source on GitHub ↗

section 15.4.4.3 Array.prototype.toLocaleString()

Source from the content-addressed store, hash-verified

163
164// section 15.4.4.3 Array.prototype.toLocaleString()
165JSVal ArrayToLocaleString(const Arguments& args, Error* e) {
166 IV_LV5_CONSTRUCTOR_CHECK("Array.prototype.toLocaleString", args, e);
167 Context* const ctx = args.ctx();
168 JSObject* const array = args.this_binding().ToObject(ctx, IV_LV5_ERROR(e));
169 const uint32_t len = internal::GetLength(ctx, array, IV_LV5_ERROR(e));
170
171 if (len == 0) {
172 return JSString::NewEmptyString(ctx);
173 }
174
175 // implementation depended locale based separator
176 const char separator = ',';
177 const Symbol toLocaleString = symbol::toLocaleString();
178 JSStringBuilder builder;
179 {
180 const JSVal first =
181 array->Get(ctx, symbol::MakeSymbolFromIndex(0u), IV_LV5_ERROR(e));
182 if (!first.IsNullOrUndefined()) {
183 JSObject* const elm_obj = first.ToObject(ctx, IV_LV5_ERROR(e));
184 const JSVal method = elm_obj->Get(ctx, toLocaleString, IV_LV5_ERROR(e));
185 if (!method.IsCallable()) {
186 e->Report(Error::Type, "toLocaleString is not function");
187 return JSUndefined;
188 }
189 ScopedArguments args_list(ctx, 0, IV_LV5_ERROR(e));
190 const JSVal R =
191 static_cast<JSFunction*>(
192 method.object())->Call(&args_list, elm_obj, IV_LV5_ERROR(e));
193 const JSString* const str = R.ToString(ctx, IV_LV5_ERROR(e));
194 builder.AppendJSString(*str);
195 }
196 }
197
198 uint32_t k = 1;
199 while (k < len) {
200 builder.Append(separator);
201 const JSVal element = array->Get(
202 ctx,
203 symbol::MakeSymbolFromIndex(k),
204 IV_LV5_ERROR(e));
205 if (!element.IsNullOrUndefined()) {
206 JSObject* const elm_obj = element.ToObject(ctx, IV_LV5_ERROR(e));
207 const JSVal method = elm_obj->Get(ctx, toLocaleString, IV_LV5_ERROR(e));
208 if (!method.IsCallable()) {
209 e->Report(Error::Type, "toLocaleString is not function");
210 return JSUndefined;
211 }
212 ScopedArguments args_list(ctx, 0, IV_LV5_ERROR(e));
213 const JSVal R =
214 static_cast<JSFunction*>(
215 method.object())->Call(&args_list, elm_obj, IV_LV5_ERROR(e));
216 const JSString* const str = R.ToString(ctx, IV_LV5_ERROR(e));
217 builder.AppendJSString(*str);
218 }
219 ++k;
220 }
221 return builder.Build(ctx, false, e);
222}

Callers

nothing calls this directly

Calls 15

GetLengthFunction · 0.85
MakeSymbolFromIndexFunction · 0.85
ToObjectMethod · 0.80
IsNullOrUndefinedMethod · 0.80
objectMethod · 0.80
AppendJSStringMethod · 0.80
ctxMethod · 0.45
this_bindingMethod · 0.45
GetMethod · 0.45
IsCallableMethod · 0.45
ReportMethod · 0.45
CallMethod · 0.45

Tested by

no test coverage detected