6"""Generates sdk/lib/_blink/dartium/_blink_dartium.dart file."""
9from generator
import AnalyzeOperation, AnalyzeConstructor, ConstantOutputOrder
15_js_custom_members =
set([
16 'Document.createElement',
20 'Element.setAttribute',
21 'Element.getAttribute',
36 'Node.previousSibling',
38 'Document.createTextNode',
49 'ParentNode.children',
50 'ParentNode.firstElementChild',
51 'ParentNode.lastElementChild',
57 'HTMLCollection.length',
58 'HTMLCollection.item',
59 'Node.lastElementChild',
60 'Node.firstElementChild',
61 'HTMLElement_tabIndex',
62 'Element.clientWidth',
63 'Element.clientHeight',
65 'Element.removeAttribute',
66 'Element.getBoundingClientRect',
67 'CSSStyleDeclaration.getPropertyValue',
68 'CSSStyleDeclaration.setProperty',
69 'CSSStyleDeclaration.__propertyQuery__',
84_additional_methods = {
89 'CSSStyleDeclaration': (
'propertyIsEnumerable', 1,
True),
92HEADER =
"""/* Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
93 * for details. All rights reserved. Use of this source code
is governed by a
94 * BSD-style license that can be found
in the LICENSE file.
97 * Auto-generated _blink library.
99library dart.dom._blink;
102import 'dart:js' as js;
103import 'dart:html' show DomException;
104import 'dart:_internal' as internal;
105// This
is a place to put custom renames
if we need them.
109dynamic resolver(String s) {
113 // Failed to find it, check
for custom renames
114 dynamic obj = resolverMap[s];
115 if (obj != null)
return obj;
116 throw(
"No such interface exposed in blink: ${s}");
122// _Utils native entry points
124 static
window() native
"Utils_window";
126 static forwardingPrint(message) native
"Utils_forwardingPrint";
128 static spawnDomUri(uri) native
"Utils_spawnDomUri";
130 static void spawnDomHelper(Function f, int replyTo) native
"Utils_spawnDomHelper";
132 static register(document, tag, customType, extendsTagName) native
"Utils_register";
134 // Below code sets up VMLibraryHooks
for resolvePackageUri.
135 static Uri resolvePackageUri(Uri packageUri) native
"Utils_resolvePackageUri";
136 static Future<Uri> _resolvePackageUriFuture(Uri packageUri)
async {
137 return resolvePackageUri(packageUri);
139 static void _setupHooks() {
140 internal.VMLibraryHooks.resolvePackageUriFuture = _resolvePackageUriFuture;
143 // Defines an interceptor
if there
is an appropriate JavaScript prototype to define it on.
144 // In any case, returns a typed JS wrapper compatible
with dart:html
and the new
146 static defineInterceptorCustomElement(jsObject, Type type) native
"Utils_defineInterceptorCustomElement";
147 static defineInterceptor(jsObject, Type type) native
"Utils_defineInterceptor";
148 static setInstanceInterceptor(o, Type type, {bool customElement: false}) native
"Utils_setInstanceInterceptor";
149 static setInstanceInterceptorCustomUpgrade(o) native
"Utils_setInstanceInterceptorCustomUpgrade";
151 // This method will throw
if the element isn
't actually a real Element.
152 static initializeCustomElement(element) native "Utils_initializeCustomElement";
155class Blink_DOMStringMap {
156 // _DOMStringMap native entry points
157 static containsKey(_DOMStringMap, key) native
"DOMStringMap_containsKey_Callback";
159 static item(_DOMStringMap, key) native
"DOMStringMap_item_Callback";
161 static setItem(_DOMStringMap, key, value) native
"DOMStringMap_setItem_Callback";
163 static
remove(_DOMStringMap, key) native
"DOMStringMap_remove_Callback";
165 static get_keys(_DOMStringMap) native
"DOMStringMap_getKeys_Callback";
168// Calls through JsNative but returns DomException instead of error strings.
171 counts = new Map<String, int>();
175 Map<String, int> counts;
181 counts[v] = counts.putIfAbsent(v, ()=> 0) + 1;
185 sb.write(
'================');
186 sb.write(
'$name ${counts.length}');
187 var keys = counts.keys.toList();
188 keys.sort((a,b) => counts[b].compareTo(counts[a]));
189 for (var key
in keys) {
190 print(
"$key => ${counts[key]}");
192 sb.write(
'---------------');
193 sb.write(
'================');
198bool TRACK_STATS = true;
200 print(
"------------ STATS ----------------");
201 print(Blink_JsNative_DomException.getPropertyStats.toString());
202 print(Blink_JsNative_DomException.setPropertyStats.toString());
203 print(Blink_JsNative_DomException.callMethodStats.toString());
204 print(Blink_JsNative_DomException.constructorStats.toString());
205 print(
"-----------------------------------");
209 Blink_JsNative_DomException.getPropertyStats.clear();
210 Blink_JsNative_DomException.setPropertyStats.clear();
211 Blink_JsNative_DomException.callMethodStats.clear();
212 Blink_JsNative_DomException.constructorStats.clear();
215class Blink_JsNative_DomException {
216 static var getPropertyStats = new
Stats(
'get property');
217 static var setPropertyStats = new
Stats(
'set property');
218 static var callMethodStats = new
Stats(
'call method');
219 static var constructorStats = new
Stats(
'constructor');
221 static var constructors = new Map<String, dynamic>();
223 static getProperty(o, String name) {
225 if (TRACK_STATS) getPropertyStats.track(name);
226 return js.JsNative.getProperty(o, name);
228 // Re-throw any errors (returned
as a string)
as a DomException.
229 throw new DomException.jsInterop(e);
233 static propertyQuery(o, String name) {
235 if (TRACK_STATS) getPropertyStats.track(
'__propertyQuery__');
236 return js.JsNative.getProperty(o, name);
238 // Re-throw any errors (returned
as a string)
as a DomException.
239 throw new DomException.jsInterop(e);
243 static callConstructor0(String name) {
245 if (TRACK_STATS) constructorStats.track(name);
246 var constructor = constructors.putIfAbsent(name, () => js.context[name]);
247 return js.JsNative.callConstructor0(constructor);
249 // Re-throw any errors (returned
as a string)
as a DomException.
250 throw new DomException.jsInterop(e);
254 static callConstructor(String name, List args) {
256 if (TRACK_STATS) constructorStats.track(name);
257 var constructor = constructors.putIfAbsent(name, () => js.context[name]);
258 return js.JsNative.callConstructor(constructor, args);
260 // Re-throw any errors (returned
as a string)
as a DomException.
261 throw new DomException.jsInterop(e);
265 static setProperty(o, String name, value) {
267 if (TRACK_STATS) setPropertyStats.track(name);
268 return js.JsNative.setProperty(o, name, value);
270 // Re-throw any errors (returned
as a string)
as a DomException.
271 throw new DomException.jsInterop(e);
275 static callMethod(o, String method, List args) {
277 if (TRACK_STATS) callMethodStats.track(method);
278 return js.JsNative.callMethod(o, method, args);
280 // Re-throw any errors (returned
as a string)
as a DomException.
281 throw new DomException.jsInterop(e);
286CLASS_DEFINITION = """class Blink%s {
287 static final instance = new Blink%
s();
291CLASS_DEFINITION_EXTENDS = """class Blink%s extends Blink%s {
292 static final instance = new Blink%
s();
300 ' constructorCallback_0_()',
301 ' => Blink_JsNative_DomException.callConstructor0("%s");\n\n',
302 ' native "Blink_Constructor_%s";\n\n'
307 ' constructorCallback_%s_(%s)',
308 ' => Blink_JsNative_DomException.callConstructor("%s", [%s]);\n\n',
309 ' native "Blink_Constructor_Args_%s" /* %s */;\n\n'
314 ' %s_Getter_(mthis)',
315 ' => Blink_JsNative_DomException.getProperty(mthis /* %s */, "%s");\n\n',
316 ' native "Blink_Getter_%s_%s";\n\n'
320 ' %s_Setter_(mthis, __arg_0)',
321 ' => Blink_JsNative_DomException.setProperty(mthis /* %s */, "%s", __arg_0);\n\n',
322 ' native "Blink_Setter_%s_%s";\n\n'
327 ' %s_Callback_0_(mthis)',
328 ' => Blink_JsNative_DomException.callMethod(mthis /* %s */, "%s", []);\n\n',
329 ' native "Blink_Operation_0_%s_%s";\n\n'
334 ' $%s_Callback_1_(mthis, __arg_0)',
335 ' => Blink_JsNative_DomException.callMethod(mthis /* %s */, "%s", [__arg_0]);\n\n',
336 ' native "Blink_Operation_1_%s_%s";\n\n'
340 ' $%s_Callback_2_(mthis, __arg_0, __arg_1)',
341 ' => Blink_JsNative_DomException.callMethod(mthis /* %s */, "%s", [__arg_0, __arg_1]);\n\n',
342 ' native "Blink_Operation_2_%s_%s";\n\n'
346 ' $%s_Callback_1_(mthis, __arg_0)',
347 ' => Blink_JsNative_DomException.propertyQuery(mthis, __arg_0); /* %s */ \n\n',
348 ' native "Blink_Operation_PQ_%s";\n\n'
352ARGUMENT_NUM =
"__arg_%s"
354 ' %s_Callback_%s_(mthis, %s)',
355 ' => Blink_JsNative_DomException.callMethod(mthis /* %s */, "%s", [%s]);\n\n',
356 ' native "Blink_Operation_%s_%s"; /* %s */\n\n'
360CLASS_STATIC =
'Blink_JsNative_DomException.getProperty(js.context, "%s")'
363STATIC_ATTRIBUTE_GETTER = [
365 ' => Blink_JsNative_DomException.getProperty(%s /* %s */, "%s");\n\n',
366 ' /* %s */ native "Blink_Static_getter_%s_%s"'
370STATIC_OPERATION_0 = [
372 ' => Blink_JsNative_DomException.callMethod(%s /* %s */, "%s", []);\n\n',
373 ' /* %s */ native "Blink_Static_Operation_0_%s_%s'
377STATIC_OPERATION_ARGS = [
378 ' %s_Callback_%s_(%s)',
379 ' => Blink_JsNative_DomException.callMethod(%s /* %s */, "%s", [%s]);\n\n',
380 ' /* %s */ native "Blink_Static_Operations_%s_%s" /* %s */ \n\n'
383CLASS_DEFINITION_END =
"""}
388 optional_default_args = 0
389 for argument
in param_infos:
390 if argument.is_optional:
391 optional_default_args += 1
393 arg_count =
len(param_infos)
394 min_arg_count = arg_count - optional_default_args
395 lb = min_arg_count - 2
if min_arg_count > 2
else 0
396 return (lb, arg_count + 1)
399constructor_renames = {
400 'RTCPeerConnection':
'webkitRTCPeerConnection',
401 'SpeechRecognition':
'webkitSpeechRecognition',
406 return constructor_renames[name]
if name
in constructor_renames
else name
409def _Find_Match(interface_id, member, member_prefix, candidates):
410 member_name = interface_id +
'.' + member
411 if member_name
in candidates:
413 member_name = interface_id +
'.' + member_prefix + member
414 if member_name
in candidates:
416 member_name = interface_id +
'.*'
417 if member_name
in candidates:
421def _Is_Native(interface, member):
422 return _Find_Match(interface, member,
'', _js_custom_members)
427 return template[0] + template[2]
429 return template[0] + template[1]
433 blink_filename = os.path.join(output_dir,
'_blink_dartium.dart')
434 blink_file = open(blink_filename,
'w')
436 blink_file.write(HEADER)
438 interfaces = database.GetInterfaces()
439 for interface
in interfaces:
441 resolver_entry =
' if (s == "%s") return Blink%s.instance;\n' % (name,
443 blink_file.write(resolver_entry)
445 blink_file.write(END_RESOLVER)
447 for interface
in interfaces:
450 if interface.parents
and len(
451 interface.parents) > 0
and interface.parents[0].id:
452 extends = interface.parents[0].id
453 class_def = CLASS_DEFINITION_EXTENDS % (name, extends, name)
455 class_def = CLASS_DEFINITION % (name, name)
456 blink_file.write(class_def)
458 analyzed_constructors = AnalyzeConstructor(interface)
459 if analyzed_constructors:
460 _Emit_Blink_Constructors(blink_file, analyzed_constructors)
461 elif 'Constructor' in interface.ext_attrs:
464 Select_Stub(CONSTRUCTOR_0, _Is_Native(name,
'constructor')) %
467 _Process_Attributes(blink_file, interface, interface.attributes)
468 _Process_Operations(blink_file, interface, interface.operations,
True)
470 _Emit_Extra_Operations(blink_file, name)
472 secondary_parents = database.TransitiveSecondaryParents(
474 for secondary
in secondary_parents:
475 _Process_Attributes(blink_file, secondary, secondary.attributes)
476 _Process_Operations(blink_file, secondary, secondary.operations,
479 blink_file.write(CLASS_DEFINITION_END)
481 blink_file.write(BLINK_UTILS)
486def _Emit_Extra_Operations(blink_file, interface_name):
487 if (interface_name
in _additional_methods):
488 (name, arg_count, return_value) = _additional_methods[interface_name]
489 exposed_name =
''.
join([
'__get',
'___', name])
if return_value
else name
491 Select_Stub(OPERATION_1,
False) % (exposed_name, interface_name,
495def _Emit_Blink_Constructors(blink_file, analyzed_constructors):
497 analyzed_constructors.param_infos)
498 name = analyzed_constructors.js_name
500 name = analyzed_constructors.type_name
502 for callback_index
in range(arg_min_count, arg_max_count):
503 if callback_index == 0:
505 Select_Stub(CONSTRUCTOR_0, _Is_Native(name,
'constructor')) %
509 for i
in range(0, callback_index):
510 arguments.append(ARGUMENT_NUM % i)
511 argument_list =
', '.
join(arguments)
513 Select_Stub(CONSTRUCTOR_ARGS, _Is_Native(name,
'constructor')) %
518def _Process_Attributes(blink_file, interface, attributes):
520 for attribute
in sorted(attributes, key=ConstantOutputOrder):
522 is_native = _Is_Native(interface.id, name)
523 if attribute.is_read_only:
524 if attribute.is_static:
525 class_property = CLASS_STATIC % interface.id
528 (name, class_property, interface.id, name))
532 (name, interface.id, name))
535 Select_Stub(ATTRIBUTE_GETTER, is_native) % (name, interface.id,
538 Select_Stub(ATTRIBUTE_SETTER, is_native) % (name, interface.id,
542def _Process_Operations(blink_file,
545 primary_interface=False):
546 analyzeOperations = []
548 for operation
in sorted(operations, key=ConstantOutputOrder):
549 if len(analyzeOperations) == 0:
550 analyzeOperations.append(operation)
552 if analyzeOperations[0].id == operation.id:
554 analyzeOperations.append(operation)
556 _Emit_Blink_Operation(blink_file, interface, analyzeOperations,
558 analyzeOperations = [operation]
559 if len(analyzeOperations) > 0:
560 _Emit_Blink_Operation(blink_file, interface, analyzeOperations,
568suppressed_operations = {
569 'WebGL2RenderingContextBase': [
'readPixels2',
'texImage2D2'],
573def _Suppress_Secondary_Interface_Operation(interface, analyzed):
574 if interface.id
in suppressed_operations:
576 return analyzed.name
in suppressed_operations[interface.id]
580def _Emit_Blink_Operation(blink_file, interface, analyzeOperations,
582 analyzed = AnalyzeOperation(interface, analyzeOperations)
584 if not (primary_interface)
and _Suppress_Secondary_Interface_Operation(
585 interface, analyzed):
590 name = analyzed.js_name
592 is_native = _Is_Native(interface.id, name)
594 operation = analyzeOperations[0]
595 if (name.startswith(
'__')
and \
596 (
'getter' in operation.specials
or \
597 'setter' in operation.specials
or \
598 'deleter' in operation.specials)):
599 if name ==
'__propertyQuery__':
601 Select_Stub(OPERATION_PQ, is_native) % (name, interface.id))
603 arg_min_count = arg_max_count
604 if arg_max_count == 2:
606 Select_Stub(OPERATION_1, is_native) % (name, interface.id,
608 elif arg_max_count == 3:
610 Select_Stub(OPERATION_2, is_native) % (name, interface.id,
613 print(
"FATAL ERROR: _blink emitter operator %s.%s" %
614 (interface.id, name))
619 for callback_index
in range(arg_min_count, arg_max_count):
620 if callback_index == 0:
621 if operation.is_static:
622 class_property = CLASS_STATIC % interface.id
625 (name, class_property, interface.id, name))
628 Select_Stub(OPERATION_0, is_native) % (name, interface.id,
632 for i
in range(0, callback_index):
633 arguments.append(ARGUMENT_NUM % i)
634 argument_list =
', '.
join(arguments)
635 if operation.is_static:
636 class_property = CLASS_STATIC % interface.id
639 (name, callback_index, argument_list, class_property,
640 interface.id, name, argument_list))
644 (name, callback_index, argument_list, interface.id, name,
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not set
def print(*args, **kwargs)
def Select_Stub(template, is_native)
def rename_constructor(name)
def generate_parameter_entries(param_infos)
def Generate_Blink(output_dir, database, type_registry)
static SkString join(const CommandLineFlags::StringArray &)