Commit bde8f574 authored by rdevlin.cronin's avatar rdevlin.cronin Committed by Commit bot

[Extension API Extern Generation] Fix array specification

Instead of simply outputting "Array", we now output the type of the array
(e.g., Array<number> or Array<MyCustomType>). Additionally, arrays should not
be nullable.

Choices (e.g., (DOMString or number) in an .idl) are also now supported.

BUG=461039

Review URL: https://codereview.chromium.org/1034553003

Cr-Commit-Position: refs/heads/master@{#322110}
parent c4c495eb
...@@ -180,14 +180,19 @@ class _Generator(object): ...@@ -180,14 +180,19 @@ class _Generator(object):
elif js_type.property_type is PropertyType.OBJECT: elif js_type.property_type is PropertyType.OBJECT:
return 'Object' return 'Object'
elif js_type.property_type is PropertyType.ARRAY: elif js_type.property_type is PropertyType.ARRAY:
return 'Array' return '!Array<%s>' % self._TypeToJsType(js_type.item_type)
elif js_type.property_type is PropertyType.REF: elif js_type.property_type is PropertyType.REF:
ref_type = js_type.ref_type ref_type = js_type.ref_type
# Enums are defined as chrome.fooAPI.MyEnum, but types are defined simply # Enums are defined as chrome.fooAPI.MyEnum, but types are defined simply
# as MyType. # as MyType.
if self._namespace.types[ref_type].property_type is PropertyType.ENUM: if self._namespace.types[ref_type].property_type is PropertyType.ENUM:
ref_type = 'chrome.%s.%s' % (self._namespace.name, ref_type) ref_type = '!chrome.%s.%s' % (self._namespace.name, ref_type)
return ref_type return ref_type
elif js_type.property_type is PropertyType.CHOICES:
return '(%s)' % '|'.join(
[self._TypeToJsType(choice) for choice in js_type.choices])
elif js_type.property_type is PropertyType.ANY:
return '*'
elif js_type.property_type.is_fundamental: elif js_type.property_type.is_fundamental:
return js_type.property_type.name return js_type.property_type.name
else: else:
......
...@@ -34,9 +34,14 @@ namespace fakeApi { ...@@ -34,9 +34,14 @@ namespace fakeApi {
long num; long num;
boolean b; boolean b;
Greek letter; Greek letter;
Greek? optionalLetter;
long[] arr; long[] arr;
Bar[]? optionalObjArr;
Greek[] enumArr;
any[] anythingGoes;
Bar obj; Bar obj;
long? maybe; long? maybe;
(DOMString or Greek or long[]) choice;
}; };
callback VoidCallback = void(); callback VoidCallback = void();
...@@ -83,10 +88,15 @@ var Bar; ...@@ -83,10 +88,15 @@ var Bar;
* str: string, * str: string,
* num: number, * num: number,
* b: boolean, * b: boolean,
* letter: chrome.fakeApi.Greek, * letter: !chrome.fakeApi.Greek,
* arr: Array, * optionalLetter: (!chrome.fakeApi.Greek|undefined),
* arr: !Array<number>,
* optionalObjArr: (!Array<Bar>|undefined),
* enumArr: !Array<!chrome.fakeApi.Greek>,
* anythingGoes: !Array<*>,
* obj: Bar, * obj: Bar,
* maybe: (number|undefined) * maybe: (number|undefined),
* choice: (string|!chrome.fakeApi.Greek|!Array<number>)
* }} * }}
*/ */
var Baz; var Baz;
...@@ -102,12 +112,13 @@ chrome.fakeApi.doSomething = function(baz, callback) {}; ...@@ -102,12 +112,13 @@ chrome.fakeApi.doSomething = function(baz, callback) {};
class JsExternGeneratorTest(unittest.TestCase): class JsExternGeneratorTest(unittest.TestCase):
def testBasic(self): def testBasic(self):
self.maxDiff = None # Lets us see the full diff when inequal.
filename = 'fake_api.idl' filename = 'fake_api.idl'
api_def = idl_schema.Process(fake_idl, filename) api_def = idl_schema.Process(fake_idl, filename)
m = model.Model() m = model.Model()
namespace = m.AddNamespace(api_def[0], filename) namespace = m.AddNamespace(api_def[0], filename)
self.assertEquals(expected_output, self.assertMultiLineEqual(expected_output,
JsExternsGenerator().Generate(namespace).Render()) JsExternsGenerator().Generate(namespace).Render())
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment