OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # |
| 3 # Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 4 # for details. All rights reserved. Use of this source code is governed by a |
| 5 # BSD-style license that can be found in the LICENSE file. |
| 6 |
| 7 """Generates sdk/lib/_blink/dartium/_blink_dartium.dart file.""" |
| 8 |
| 9 import os |
| 10 |
| 11 from generator import AnalyzeOperation, AnalyzeConstructor |
| 12 |
| 13 HEADER = """/* Copyright (c) 2014, the Dart project authors. Please see the AUT
HORS file |
| 14 * for details. All rights reserved. Use of this source code is governed by a |
| 15 * BSD-style license that can be found in the LICENSE file. |
| 16 * |
| 17 * DO NOT EDIT |
| 18 * Auto-generated _blink library. |
| 19 */ |
| 20 library dart.dom._blink; |
| 21 |
| 22 import 'dart:js' as js; |
| 23 import 'dart:html' show DomException; |
| 24 // This is a place to put custom renames if we need them. |
| 25 final resolverMap = { |
| 26 }; |
| 27 |
| 28 dynamic resolver(String s) { |
| 29 """ |
| 30 |
| 31 END_RESOLVER = """ |
| 32 // Failed to find it, check for custom renames |
| 33 dynamic obj = resolverMap[s]; |
| 34 if (obj != null) return obj; |
| 35 throw("No such interface exposed in blink: ${s}"); |
| 36 } |
| 37 |
| 38 """ |
| 39 |
| 40 BLINK_UTILS = """ |
| 41 // _Utils native entry points |
| 42 class Blink_Utils { |
| 43 static window() native "Utils_window"; |
| 44 |
| 45 static forwardingPrint(message) native "Utils_forwardingPrint"; |
| 46 |
| 47 static spawnDomUri(uri) native "Utils_spawnDomUri"; |
| 48 |
| 49 static void spawnDomHelper(Function f, int replyTo) native "Utils_spawnDomHelp
er"; |
| 50 |
| 51 static register(document, tag, customType, extendsTagName) native "Utils_regis
ter"; |
| 52 |
| 53 static createElement(document, tagName) native "Utils_createElement"; |
| 54 |
| 55 static constructElement(element_type, jsObject) native "Utils_constructor_crea
te"; |
| 56 |
| 57 static initializeCustomElement(element) native "Utils_initializeCustomElement"
; |
| 58 |
| 59 static changeElementWrapper(element, type) native "Utils_changeElementWrapper"
; |
| 60 } |
| 61 |
| 62 class Blink_DOMWindowCrossFrame { |
| 63 // FIXME: Return to using explicit cross frame entry points after roll to M35 |
| 64 static get_history(_DOMWindowCrossFrame) native "Window_history_cross_frame_Ge
tter"; |
| 65 |
| 66 static get_location(_DOMWindowCrossFrame) native "Window_location_cross_frame_
Getter"; |
| 67 |
| 68 static get_closed(_DOMWindowCrossFrame) native "Window_closed_Getter"; |
| 69 |
| 70 static get_opener(_DOMWindowCrossFrame) native "Window_opener_Getter"; |
| 71 |
| 72 static get_parent(_DOMWindowCrossFrame) native "Window_parent_Getter"; |
| 73 |
| 74 static get_top(_DOMWindowCrossFrame) native "Window_top_Getter"; |
| 75 |
| 76 static close(_DOMWindowCrossFrame) native "Window_close_Callback"; |
| 77 |
| 78 static postMessage(_DOMWindowCrossFrame, message, targetOrigin, [messagePorts]
) native "Window_postMessage_Callback"; |
| 79 } |
| 80 |
| 81 class Blink_HistoryCrossFrame { |
| 82 // _HistoryCrossFrame native entry points |
| 83 static back(_HistoryCrossFrame) native "History_back_Callback"; |
| 84 |
| 85 static forward(_HistoryCrossFrame) native "History_forward_Callback"; |
| 86 |
| 87 static go(_HistoryCrossFrame, distance) native "History_go_Callback"; |
| 88 } |
| 89 |
| 90 class Blink_LocationCrossFrame { |
| 91 // _LocationCrossFrame native entry points |
| 92 static set_href(_LocationCrossFrame, h) native "Location_href_Setter"; |
| 93 } |
| 94 |
| 95 class Blink_DOMStringMap { |
| 96 // _DOMStringMap native entry points |
| 97 static containsKey(_DOMStringMap, key) native "DOMStringMap_containsKey_Callba
ck"; |
| 98 |
| 99 static item(_DOMStringMap, key) native "DOMStringMap_item_Callback"; |
| 100 |
| 101 static setItem(_DOMStringMap, key, value) native "DOMStringMap_setItem_Callbac
k"; |
| 102 |
| 103 static remove(_DOMStringMap, key) native "DOMStringMap_remove_Callback"; |
| 104 |
| 105 static get_keys(_DOMStringMap) native "DOMStringMap_getKeys_Callback"; |
| 106 } |
| 107 |
| 108 // Calls through JsNative but returns DomException instead of error strings. |
| 109 class Blink_JsNative_DomException { |
| 110 static getProperty(js.JsObject o, name) { |
| 111 try { |
| 112 return js.JsNative.getProperty(o, name); |
| 113 } catch (e) { |
| 114 // Re-throw any errors (returned as a string) as a DomException. |
| 115 throw new DomException.jsInterop(e); |
| 116 } |
| 117 } |
| 118 |
| 119 static callMethod(js.JsObject o, String method, List args) { |
| 120 try { |
| 121 return js.JsNative.callMethod(o, method, args); |
| 122 } catch (e) { |
| 123 // Re-throw any errors (returned as a string) as a DomException. |
| 124 throw new DomException.jsInterop(e); |
| 125 } |
| 126 } |
| 127 }""" |
| 128 |
| 129 CLASS_DEFINITION = """class Blink%s { |
| 130 static final instance = new Blink%s(); |
| 131 |
| 132 """ |
| 133 |
| 134 CLASS_DEFINITION_EXTENDS = """class Blink%s extends Blink%s { |
| 135 static final instance = new Blink%s(); |
| 136 |
| 137 """ |
| 138 |
| 139 #(interface_name) |
| 140 CONSTRUCTOR_0 = ' constructorCallback_0_() => new js.JsObject(Blink_JsNative_Do
mException.getProperty(js.context, "%s"), []);\n\n' |
| 141 |
| 142 #(argument_count, arguments, interface_name, arguments) |
| 143 CONSTRUCTOR_ARGS = ' constructorCallback_%s_(%s) => new js.JsObject(Blink_JsNat
ive_DomException.getProperty(js.context, "%s"), [%s]);\n\n' |
| 144 |
| 145 #(attribute_name, attribute_name) |
| 146 ATTRIBUTE_GETTER = ' %s_Getter_(mthis) => Blink_JsNative_DomException.getProper
ty(mthis, "%s");\n\n' |
| 147 ATTRIBUTE_SETTER = ' %s_Setter_(mthis, __arg_0) => mthis["%s"] = __arg_0;\n\n' |
| 148 |
| 149 #(operation_name, operationName) |
| 150 OPERATION_0 = ' %s_Callback_0_(mthis) => Blink_JsNative_DomException.callMethod
(mthis, "%s", []);\n\n' |
| 151 |
| 152 #(operation_name, argument_count, arguments, operation_name, arguments) |
| 153 ARGUMENT_NUM = "__arg_%s" |
| 154 OPERATION_ARGS = ' %s_Callback_%s_(mthis, %s) => Blink_JsNative_DomException.ca
llMethod(mthis, "%s", [%s]);\n\n' |
| 155 |
| 156 CLASS_DEFINITION_END = """} |
| 157 |
| 158 """ |
| 159 |
| 160 def ConstantOutputOrder(a, b): |
| 161 """Canonical output ordering for constants.""" |
| 162 return cmp(a.id, b.id) |
| 163 |
| 164 def generate_parameter_entries(param_infos): |
| 165 optional_default_args = 0; |
| 166 for argument in param_infos: |
| 167 if argument.is_optional: |
| 168 optional_default_args += 1 |
| 169 |
| 170 arg_count = len(param_infos) |
| 171 min_arg_count = arg_count - optional_default_args |
| 172 lb = min_arg_count - 2 if min_arg_count > 2 else 0 |
| 173 return (lb, arg_count + 1) |
| 174 |
| 175 def Generate_Blink(output_dir, database, type_registry): |
| 176 blink_filename = os.path.join(output_dir, '_blink_dartium.dart') |
| 177 blink_file = open(blink_filename, 'w') |
| 178 |
| 179 blink_file.write(HEADER); |
| 180 |
| 181 interfaces = database.GetInterfaces() |
| 182 for interface in interfaces: |
| 183 name = interface.id |
| 184 resolver_entry = ' if (s == "%s") return Blink%s.instance;\n' % (name, name
) |
| 185 blink_file.write(resolver_entry) |
| 186 |
| 187 blink_file.write(END_RESOLVER); |
| 188 |
| 189 for interface in interfaces: |
| 190 name = interface.id |
| 191 |
| 192 if interface.parents and len(interface.parents) > 0 and interface.parents[0]
.id: |
| 193 extends = interface.parents[0].id |
| 194 class_def = CLASS_DEFINITION_EXTENDS % (name, extends, name) |
| 195 else: |
| 196 class_def = CLASS_DEFINITION % (name, name) |
| 197 blink_file.write(class_def); |
| 198 |
| 199 analyzed_constructors = AnalyzeConstructor(interface) |
| 200 if analyzed_constructors: |
| 201 _Emit_Blink_Constructors(blink_file, analyzed_constructors) |
| 202 elif 'Constructor' in interface.ext_attrs: |
| 203 # Zero parameter constructor. |
| 204 blink_file.write(CONSTRUCTOR_0 % name) |
| 205 |
| 206 _Process_Attributes(blink_file, interface.attributes) |
| 207 _Process_Operations(blink_file, interface, interface.operations) |
| 208 |
| 209 secondary_parents = database.TransitiveSecondaryParents(interface, False) |
| 210 for secondary in secondary_parents: |
| 211 _Process_Attributes(blink_file, secondary.attributes) |
| 212 _Process_Operations(blink_file, secondary, secondary.operations) |
| 213 |
| 214 blink_file.write(CLASS_DEFINITION_END); |
| 215 |
| 216 blink_file.write(BLINK_UTILS) |
| 217 |
| 218 blink_file.close() |
| 219 |
| 220 def _Emit_Blink_Constructors(blink_file, analyzed_constructors): |
| 221 (arg_min_count, arg_max_count) = generate_parameter_entries(analyzed_construct
ors.param_infos) |
| 222 name = analyzed_constructors.js_name |
| 223 if not(name): |
| 224 name = analyzed_constructors.type_name |
| 225 |
| 226 for callback_index in range(arg_min_count, arg_max_count): |
| 227 if callback_index == 0: |
| 228 blink_file.write(CONSTRUCTOR_0 % (name)) |
| 229 else: |
| 230 arguments = [] |
| 231 for i in range(0, callback_index): |
| 232 arguments.append(ARGUMENT_NUM % i) |
| 233 argument_list = ', '.join(arguments) |
| 234 blink_file.write(CONSTRUCTOR_ARGS % (callback_index, argument_list, name,
argument_list)) |
| 235 |
| 236 def _Process_Attributes(blink_file, attributes): |
| 237 # Emit an interface's attributes and operations. |
| 238 for attribute in sorted(attributes, ConstantOutputOrder): |
| 239 name = attribute.id |
| 240 if attribute.is_read_only: |
| 241 blink_file.write(ATTRIBUTE_GETTER % (name, name)) |
| 242 else: |
| 243 blink_file.write(ATTRIBUTE_GETTER % (name, name)) |
| 244 blink_file.write(ATTRIBUTE_SETTER % (name, name)) |
| 245 |
| 246 def _Process_Operations(blink_file, interface, operations): |
| 247 analyzeOperations = [] |
| 248 |
| 249 for operation in sorted(operations, ConstantOutputOrder): |
| 250 if len(analyzeOperations) == 0: |
| 251 analyzeOperations.append(operation) |
| 252 else: |
| 253 if analyzeOperations[0].id == operation.id: |
| 254 # Handle overloads |
| 255 analyzeOperations.append(operation) |
| 256 else: |
| 257 _Emit_Blink_Operation(blink_file, interface, analyzeOperations) |
| 258 analyzeOperations = [operation] |
| 259 if len(analyzeOperations) > 0: |
| 260 _Emit_Blink_Operation(blink_file, interface, analyzeOperations) |
| 261 |
| 262 def _Emit_Blink_Operation(blink_file, interface, analyzeOperations): |
| 263 analyzed = AnalyzeOperation(interface, analyzeOperations) |
| 264 (arg_min_count, arg_max_count) = generate_parameter_entries(analyzed.param_inf
os) |
| 265 name = analyzed.js_name |
| 266 |
| 267 for callback_index in range(arg_min_count, arg_max_count): |
| 268 if callback_index == 0: |
| 269 blink_file.write(OPERATION_0 % (name, name)) |
| 270 else: |
| 271 arguments = [] |
| 272 for i in range(0, callback_index): |
| 273 arguments.append(ARGUMENT_NUM % i) |
| 274 argument_list = ', '.join(arguments) |
| 275 blink_file.write(OPERATION_ARGS % (name, callback_index, argument_list, na
me, argument_list)) |
OLD | NEW |