GeezerVison is showing github.com — src: use DictionaryTemplate for node_url_pattern · nodejs/node@92128a8
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/env_properties.h +12-48Lines changed: 12 additions & 48 deletions Large diffs are not rendered by default. Collapse file Copy file name to clipboardExpand all lines: src/node_sqlite.cc +68-72Lines changed: 68 additions & 72 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -24,6 +24,7 @@ using v8::BigInt; | 24 | 24 | using v8::Boolean; | 25 | 25 | using v8::ConstructorBehavior; | 26 | 26 | using v8::Context; | | 27 | +using v8::DictionaryTemplate; | 27 | 28 | using v8::DontDelete; | 28 | 29 | using v8::Exception; | 29 | 30 | using v8::Function; | @@ -119,6 +120,18 @@ using v8::Value; | 119 | 120 | } \ | 120 | 121 | } while (0) | 121 | 122 |
| | 123 | +namespace { | | 124 | +Local<DictionaryTemplate> getLazyIterTemplate(Environment* env) { | | 125 | + auto iter_template = env->iter_template(); | | 126 | + if (iter_template.IsEmpty()) { | | 127 | + static constexpr std::string_view iter_keys[] = {"done", "value"}; | | 128 | + iter_template = DictionaryTemplate::New(env->isolate(), iter_keys); | | 129 | + env->set_iter_template(iter_template); | | 130 | + } | | 131 | + return iter_template; | | 132 | +} | | 133 | +} // namespace | | 134 | + | 122 | 135 | inline MaybeLocal<Object> CreateSQLiteError(Isolate* isolate, | 123 | 136 | const char* message) { | 124 | 137 | Local<String> js_msg; | @@ -2231,58 +2244,35 @@ void StatementSync::Columns(const FunctionCallbackInfo<Value>& args) { | 2231 | 2244 | int num_cols = sqlite3_column_count(stmt->statement_); | 2232 | 2245 | Isolate* isolate = env->isolate(); | 2233 | 2246 | LocalVector<Value> cols(isolate); | 2234 | | - LocalVector<Name> col_keys(isolate, | 2235 | | - {env->column_string(), | 2236 | | - env->database_string(), | 2237 | | - env->name_string(), | 2238 | | - env->table_string(), | 2239 | | - env->type_string()}); | 2240 | | - Local<Value> value; | | 2247 | + auto sqlite_column_template = env->sqlite_column_template(); | | 2248 | + if (sqlite_column_template.IsEmpty()) { | | 2249 | + static constexpr std::string_view col_keys[] = { | | 2250 | + "column", "database", "name", "table", "type"}; | | 2251 | + sqlite_column_template = DictionaryTemplate::New(isolate, col_keys); | | 2252 | + env->set_sqlite_column_template(sqlite_column_template); | | 2253 | + } | 2241 | 2254 |
| 2242 | 2255 | cols.reserve(num_cols); | 2243 | 2256 | for (int i = 0; i < num_cols; ++i) { | 2244 | | - LocalVector<Value> col_values(isolate); | 2245 | | - col_values.reserve(col_keys.size()); | 2246 | | - | 2247 | | - if (!NullableSQLiteStringToValue( | 2248 | | - isolate, sqlite3_column_origin_name(stmt->statement_, i)) | 2249 | | - .ToLocal(&value)) { | 2250 | | - return; | 2251 | | - } | 2252 | | - col_values.emplace_back(value); | 2253 | | - | 2254 | | - if (!NullableSQLiteStringToValue( | 2255 | | - isolate, sqlite3_column_database_name(stmt->statement_, i)) | 2256 | | - .ToLocal(&value)) { | 2257 | | - return; | 2258 | | - } | 2259 | | - col_values.emplace_back(value); | 2260 | | - | 2261 | | - if (!stmt->ColumnNameToName(i).ToLocal(&value)) { | 2262 | | - return; | 2263 | | - } | 2264 | | - col_values.emplace_back(value); | 2265 | | - | 2266 | | - if (!NullableSQLiteStringToValue( | 2267 | | - isolate, sqlite3_column_table_name(stmt->statement_, i)) | 2268 | | - .ToLocal(&value)) { | 2269 | | - return; | 2270 | | - } | 2271 | | - col_values.emplace_back(value); | 2272 | | - | 2273 | | - if (!NullableSQLiteStringToValue( | 2274 | | - isolate, sqlite3_column_decltype(stmt->statement_, i)) | 2275 | | - .ToLocal(&value)) { | | 2257 | + MaybeLocal<Value> values[] = { | | 2258 | + NullableSQLiteStringToValue( | | 2259 | + isolate, sqlite3_column_origin_name(stmt->statement_, i)), | | 2260 | + NullableSQLiteStringToValue( | | 2261 | + isolate, sqlite3_column_database_name(stmt->statement_, i)), | | 2262 | + stmt->ColumnNameToName(i), | | 2263 | + NullableSQLiteStringToValue( | | 2264 | + isolate, sqlite3_column_table_name(stmt->statement_, i)), | | 2265 | + NullableSQLiteStringToValue( | | 2266 | + isolate, sqlite3_column_decltype(stmt->statement_, i)), | | 2267 | + }; | | 2268 | + | | 2269 | + Local<Object> col; | | 2270 | + if (!NewDictionaryInstanceNullProto( | | 2271 | + env->context(), sqlite_column_template, values) | | 2272 | + .ToLocal(&col)) { | 2276 | 2273 | return; | 2277 | 2274 | } | 2278 | | - col_values.emplace_back(value); | 2279 | | - | 2280 | | - Local<Object> column = Object::New(isolate, | 2281 | | - Null(isolate), | 2282 | | - col_keys.data(), | 2283 | | - col_values.data(), | 2284 | | - col_keys.size()); | 2285 | | - cols.emplace_back(column); | | 2275 | + cols.emplace_back(col); | 2286 | 2276 | } | 2287 | 2277 |
| 2288 | 2278 | args.GetReturnValue().Set(Array::New(isolate, cols.data(), cols.size())); | @@ -2514,15 +2504,19 @@ void StatementSyncIterator::Next(const FunctionCallbackInfo<Value>& args) { | 2514 | 2504 | THROW_AND_RETURN_ON_BAD_STATE( | 2515 | 2505 | env, iter->stmt_->IsFinalized(), "statement has been finalized"); | 2516 | 2506 | Isolate* isolate = env->isolate(); | 2517 | | - LocalVector<Name> keys(isolate, {env->done_string(), env->value_string()}); | | 2507 | + | | 2508 | + auto iter_template = getLazyIterTemplate(env); | 2518 | 2509 |
| 2519 | 2510 | if (iter->done_) { | 2520 | | - LocalVector<Value> values(isolate, | 2521 | | - {Boolean::New(isolate, true), Null(isolate)}); | 2522 | | - DCHECK_EQ(values.size(), keys.size()); | 2523 | | - Local<Object> result = Object::New( | 2524 | | - isolate, Null(isolate), keys.data(), values.data(), keys.size()); | 2525 | | - args.GetReturnValue().Set(result); | | 2511 | + MaybeLocal<Value> values[]{ | | 2512 | + Boolean::New(isolate, true), | | 2513 | + Null(isolate), | | 2514 | + }; | | 2515 | + Local<Object> result; | | 2516 | + if (NewDictionaryInstanceNullProto(env->context(), iter_template, values) | | 2517 | + .ToLocal(&result)) { | | 2518 | + args.GetReturnValue().Set(result); | | 2519 | + } | 2526 | 2520 | return; | 2527 | 2521 | } | 2528 | 2522 |
| @@ -2531,12 +2525,12 @@ void StatementSyncIterator::Next(const FunctionCallbackInfo<Value>& args) { | 2531 | 2525 | CHECK_ERROR_OR_THROW( | 2532 | 2526 | env->isolate(), iter->stmt_->db_.get(), r, SQLITE_DONE, void()); | 2533 | 2527 | sqlite3_reset(iter->stmt_->statement_); | 2534 | | - LocalVector<Value> values(isolate, | 2535 | | - {Boolean::New(isolate, true), Null(isolate)}); | 2536 | | - DCHECK_EQ(values.size(), keys.size()); | 2537 | | - Local<Object> result = Object::New( | 2538 | | - isolate, Null(isolate), keys.data(), values.data(), keys.size()); | 2539 | | - args.GetReturnValue().Set(result); | | 2528 | + MaybeLocal<Value> values[] = {Boolean::New(isolate, true), Null(isolate)}; | | 2529 | + Local<Object> result; | | 2530 | + if (NewDictionaryInstanceNullProto(env->context(), iter_template, values) | | 2531 | + .ToLocal(&result)) { | | 2532 | + args.GetReturnValue().Set(result); | | 2533 | + } | 2540 | 2534 | return; | 2541 | 2535 | } | 2542 | 2536 |
| @@ -2564,11 +2558,12 @@ void StatementSyncIterator::Next(const FunctionCallbackInfo<Value>& args) { | 2564 | 2558 | isolate, Null(isolate), row_keys.data(), row_values.data(), num_cols); | 2565 | 2559 | } | 2566 | 2560 |
| 2567 | | - LocalVector<Value> values(isolate, {Boolean::New(isolate, false), row_value}); | 2568 | | - DCHECK_EQ(keys.size(), values.size()); | 2569 | | - Local<Object> result = Object::New( | 2570 | | - isolate, Null(isolate), keys.data(), values.data(), keys.size()); | 2571 | | - args.GetReturnValue().Set(result); | | 2561 | + MaybeLocal<Value> values[] = {Boolean::New(isolate, false), row_value}; | | 2562 | + Local<Object> result; | | 2563 | + if (NewDictionaryInstanceNullProto(env->context(), iter_template, values) | | 2564 | + .ToLocal(&result)) { | | 2565 | + args.GetReturnValue().Set(result); | | 2566 | + } | 2572 | 2567 | } | 2573 | 2568 |
| 2574 | 2569 | void StatementSyncIterator::Return(const FunctionCallbackInfo<Value>& args) { | @@ -2581,14 +2576,15 @@ void StatementSyncIterator::Return(const FunctionCallbackInfo<Value>& args) { | 2581 | 2576 |
| 2582 | 2577 | sqlite3_reset(iter->stmt_->statement_); | 2583 | 2578 | iter->done_ = true; | 2584 | | - LocalVector<Name> keys(isolate, {env->done_string(), env->value_string()}); | 2585 | | - LocalVector<Value> values(isolate, | 2586 | | - {Boolean::New(isolate, true), Null(isolate)}); | 2587 | 2579 |
| 2588 | | - DCHECK_EQ(keys.size(), values.size()); | 2589 | | - Local<Object> result = Object::New( | 2590 | | - isolate, Null(isolate), keys.data(), values.data(), keys.size()); | 2591 | | - args.GetReturnValue().Set(result); | | 2580 | + auto iter_template = getLazyIterTemplate(env); | | 2581 | + MaybeLocal<Value> values[] = {Boolean::New(isolate, true), Null(isolate)}; | | 2582 | + | | 2583 | + Local<Object> result; | | 2584 | + if (NewDictionaryInstanceNullProto(env->context(), iter_template, values) | | 2585 | + .ToLocal(&result)) { | | 2586 | + args.GetReturnValue().Set(result); | | 2587 | + } | 2592 | 2588 | } | 2593 | 2589 |
| 2594 | 2590 | Session::Session(Environment* env, | |
Collapse file Copy file name to clipboardExpand all lines: src/node_url_pattern.cc +43-50Lines changed: 43 additions & 50 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -54,13 +54,13 @@ namespace node::url_pattern { | 54 | 54 |
| 55 | 55 | using v8::Array; | 56 | 56 | using v8::Context; | | 57 | +using v8::DictionaryTemplate; | 57 | 58 | using v8::DontDelete; | 58 | 59 | using v8::FunctionCallbackInfo; | 59 | 60 | using v8::FunctionTemplate; | 60 | 61 | using v8::Global; | 61 | 62 | using v8::Isolate; | 62 | 63 | using v8::Local; | 63 | | -using v8::LocalVector; | 64 | 64 | using v8::MaybeLocal; | 65 | 65 | using v8::Name; | 66 | 66 | using v8::NewStringType; | @@ -396,56 +396,49 @@ MaybeLocal<Object> URLPattern::URLPatternComponentResult::ToJSObject( | 396 | 396 | MaybeLocal<Value> URLPattern::URLPatternResult::ToJSValue( | 397 | 397 | Environment* env, const ada::url_pattern_result& result) { | 398 | 398 | auto isolate = env->isolate(); | 399 | | - Local<Name> names[] = { | 400 | | - env->inputs_string(), | 401 | | - env->protocol_string(), | 402 | | - env->username_string(), | 403 | | - env->password_string(), | 404 | | - env->hostname_string(), | 405 | | - env->port_string(), | 406 | | - env->pathname_string(), | 407 | | - env->search_string(), | 408 | | - env->hash_string(), | 409 | | - }; | 410 | | - LocalVector<Value> inputs(isolate, result.inputs.size()); | 411 | | - size_t index = 0; | 412 | | - for (auto& input : result.inputs) { | 413 | | - if (std::holds_alternative<std::string_view>(input)) { | 414 | | - auto input_str = std::get<std::string_view>(input); | 415 | | - if (!ToV8Value(env->context(), input_str).ToLocal(&inputs[index])) { | 416 | | - return {}; | 417 | | - } | 418 | | - } else { | 419 | | - DCHECK(std::holds_alternative<ada::url_pattern_init>(input)); | 420 | | - auto init = std::get<ada::url_pattern_init>(input); | 421 | | - if (!URLPatternInit::ToJsObject(env, init).ToLocal(&inputs[index])) { | 422 | | - return {}; | 423 | | - } | 424 | | - } | 425 | | - index++; | 426 | | - } | 427 | | - LocalVector<Value> values(isolate, arraysize(names)); | 428 | | - values[0] = Array::New(isolate, inputs.data(), inputs.size()); | 429 | | - if (!URLPatternComponentResult::ToJSObject(env, result.protocol) | 430 | | - .ToLocal(&values[1]) || | 431 | | - !URLPatternComponentResult::ToJSObject(env, result.username) | 432 | | - .ToLocal(&values[2]) || | 433 | | - !URLPatternComponentResult::ToJSObject(env, result.password) | 434 | | - .ToLocal(&values[3]) || | 435 | | - !URLPatternComponentResult::ToJSObject(env, result.hostname) | 436 | | - .ToLocal(&values[4]) || | 437 | | - !URLPatternComponentResult::ToJSObject(env, result.port) | 438 | | - .ToLocal(&values[5]) || | 439 | | - !URLPatternComponentResult::ToJSObject(env, result.pathname) | 440 | | - .ToLocal(&values[6]) || | 441 | | - !URLPatternComponentResult::ToJSObject(env, result.search) | 442 | | - .ToLocal(&values[7]) || | 443 | | - !URLPatternComponentResult::ToJSObject(env, result.hash) | 444 | | - .ToLocal(&values[8])) { | 445 | | - return {}; | | 399 | + | | 400 | + auto tmpl = env->urlpatternresult_template(); | | 401 | + if (tmpl.IsEmpty()) { | | 402 | + static constexpr std::string_view namesVec[] = { | | 403 | + "inputs", | | 404 | + "protocol", | | 405 | + "username", | | 406 | + "password", | | 407 | + "hostname", | | 408 | + "port", | | 409 | + "pathname", | | 410 | + "search", | | 411 | + "hash", | | 412 | + }; | | 413 | + tmpl = DictionaryTemplate::New(isolate, namesVec); | | 414 | + env->set_urlpatternresult_template(tmpl); | 446 | 415 | } | 447 | | - return Object::New( | 448 | | - isolate, Object::New(isolate), names, values.data(), values.size()); | | 416 | + | | 417 | + size_t index = 0; | | 418 | + MaybeLocal<Value> vals[] = { | | 419 | + Array::New(env->context(), | | 420 | + result.inputs.size(), | | 421 | + [&index, &inputs = result.inputs, env]() { | | 422 | + auto& input = inputs[index++]; | | 423 | + if (std::holds_alternative<std::string_view>(input)) { | | 424 | + auto input_str = std::get<std::string_view>(input); | | 425 | + return ToV8Value(env->context(), input_str); | | 426 | + } else { | | 427 | + DCHECK( | | 428 | + std::holds_alternative<ada::url_pattern_init>(input)); | | 429 | + auto init = std::get<ada::url_pattern_init>(input); | | 430 | + return URLPatternInit::ToJsObject(env, init); | | 431 | + } | | 432 | + }), | | 433 | + URLPatternComponentResult::ToJSObject(env, result.protocol), | | 434 | + URLPatternComponentResult::ToJSObject(env, result.username), | | 435 | + URLPatternComponentResult::ToJSObject(env, result.password), | | 436 | + URLPatternComponentResult::ToJSObject(env, result.hostname), | | 437 | + URLPatternComponentResult::ToJSObject(env, result.port), | | 438 | + URLPatternComponentResult::ToJSObject(env, result.pathname), | | 439 | + URLPatternComponentResult::ToJSObject(env, result.search), | | 440 | + URLPatternComponentResult::ToJSObject(env, result.hash)}; | | 441 | + return NewDictionaryInstanceNullProto(env->context(), tmpl, vals); | 449 | 442 | } | 450 | 443 |
| 451 | 444 | std::optional<ada::url_pattern_options> | |
Collapse file Copy file name to clipboardExpand all lines: src/node_util.cc +24-13Lines changed: 24 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,6 +15,7 @@ using v8::BigInt; | 15 | 15 | using v8::Boolean; | 16 | 16 | using v8::CFunction; | 17 | 17 | using v8::Context; | | 18 | +using v8::DictionaryTemplate; | 18 | 19 | using v8::External; | 19 | 20 | using v8::FunctionCallbackInfo; | 20 | 21 | using v8::IndexFilter; | @@ -23,6 +24,7 @@ using v8::Isolate; | 23 | 24 | using v8::KeyCollectionMode; | 24 | 25 | using v8::Local; | 25 | 26 | using v8::LocalVector; | | 27 | +using v8::MaybeLocal; | 26 | 28 | using v8::Name; | 27 | 29 | using v8::Object; | 28 | 30 | using v8::ObjectTemplate; | @@ -263,6 +265,20 @@ static void GetCallSites(const FunctionCallbackInfo<Value>& args) { | 263 | 265 | const int frame_count = stack->GetFrameCount(); | 264 | 266 | LocalVector<Value> callsite_objects(isolate); | 265 | 267 |
| | 268 | + auto callsite_template = env->callsite_template(); | | 269 | + if (callsite_template.IsEmpty()) { | | 270 | + static constexpr std::string_view names[] = { | | 271 | + "functionName", | | 272 | + "scriptId", | | 273 | + "scriptName", | | 274 | + "lineNumber", | | 275 | + "columnNumber", | | 276 | + // TODO(legendecas): deprecate CallSite.column. | | 277 | + "column"}; | | 278 | + callsite_template = DictionaryTemplate::New(isolate, names); | | 279 | + env->set_callsite_template(callsite_template); | | 280 | + } | | 281 | + | 266 | 282 | // Frame 0 is node:util. It should be skipped. | 267 | 283 | for (int i = 1; i < frame_count; ++i) { | 268 | 284 | Local<StackFrame> stack_frame = stack->GetFrame(isolate, i); | @@ -279,16 +295,7 @@ static void GetCallSites(const FunctionCallbackInfo<Value>& args) { | 279 | 295 |
| 280 | 296 | std::string script_id = std::to_string(stack_frame->GetScriptId()); | 281 | 297 |
| 282 | | - Local<Name> names[] = { | 283 | | - env->function_name_string(), | 284 | | - env->script_id_string(), | 285 | | - env->script_name_string(), | 286 | | - env->line_number_string(), | 287 | | - env->column_number_string(), | 288 | | - // TODO(legendecas): deprecate CallSite.column. | 289 | | - env->column_string(), | 290 | | - }; | 291 | | - Local<Value> values[] = { | | 298 | + MaybeLocal<Value> values[] = { | 292 | 299 | function_name, | 293 | 300 | OneByteString(isolate, script_id), | 294 | 301 | script_name, | @@ -297,10 +304,14 @@ static void GetCallSites(const FunctionCallbackInfo<Value>& args) { | 297 | 304 | // TODO(legendecas): deprecate CallSite.column. | 298 | 305 | Integer::NewFromUnsigned(isolate, stack_frame->GetColumn()), | 299 | 306 | }; | 300 | | - Local<Object> obj = Object::New( | 301 | | - isolate, v8::Null(isolate), names, values, arraysize(names)); | 302 | 307 |
| 303 | | - callsite_objects.push_back(obj); | | 308 | + Local<Object> callsite; | | 309 | + if (!NewDictionaryInstanceNullProto( | | 310 | + env->context(), callsite_template, values) | | 311 | + .ToLocal(&callsite)) { | | 312 | + return; | | 313 | + } | | 314 | + callsite_objects.push_back(callsite); | 304 | 315 | } | 305 | 316 |
| 306 | 317 | Local<Array> callsites = | |
|