Commit f07d80c5 authored by Giovanni Ortuño Urquidi's avatar Giovanni Ortuño Urquidi Committed by Commit Bot

mojo-ts: Add constants to generated TS bindings

Adds support for simple constants.

Example generated JS:

var mojo;
(function (mojo) {
    var tstest;
    (function (tstest) {
        tstest.kBoolValue = true;
        tstest.kInt8Value = -2;
    })(tstest = mojo.tstest || (mojo.tstest = {}));
})(mojo || (mojo = {})); // namespace mojo.tstest



Bug: 1002798
Change-Id: I31e15f204c541e563fcb5303657c3f6507bcdc19
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1827273Reviewed-by: default avatarKen Rockot <rockot@google.com>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Commit-Queue: Giovanni Ortuño Urquidi <ortuno@chromium.org>
Cr-Commit-Position: refs/heads/master@{#702349}
parent 02d7b67a
......@@ -1047,6 +1047,7 @@ if (!is_ios) {
"//media/midi:mojo_js_data_deps",
"//mojo/public/interfaces/bindings/tests:test_data_deps",
"//mojo/public/mojom/base:base_js_data_deps",
"//mojo/public/js/ts/bindings/tests:test_interfaces_js_data_deps",
"//services/shape_detection/public/mojom:mojom_js_data_deps",
"//services/device/public/mojom:mojom_js_data_deps",
"//services/device/public/mojom:generic_sensor_js_data_deps",
......
......@@ -9,6 +9,7 @@ import("//mojo/public/tools/bindings/mojom.gni")
mojom("test_interfaces") {
testonly = true
sources = [
"constants.test-mojom",
"module.test-mojom",
]
use_typescript_sources = true
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
module mojo.tstest;
// Integral types.
const bool kBoolValue = true;
const int8 kInt8Value = -2;
// In the range of (MAX_INT8, MAX_UINT8].
const uint8 kUint8Value = 128;
// In the range of [MIN_INT16, MIN_INT8).
const int16 kInt16Value = -233;
// In the range of (MAX_INT16, MAX_UINT16].
const uint16 kUint16Value = 44204;
// In the range of [MIN_INT32, MIN_INT16).
const int32 kInt32Value = -44204;
// In the range of (MAX_INT32, MAX_UINT32].
const uint32 kUint32Value = 4294967295;
// In the range of [MIN_INT64, MIN_INT32).
const int64 kInt64Value = -9223372036854775807;
// In the range of (MAX_INT64, MAX_UINT64].
const uint64 kUint64Value = 9999999999999999999;
/// Floating point types.
const double kDoubleValue = 3.14159;
const double kDoubleInfinity = double.INFINITY;
const double kDoubleNegativeInfinity = double.NEGATIVE_INFINITY;
const double kDoubleNaN = double.NAN;
const float kFloatValue = 2.71828;
const float kFloatInfinity = float.INFINITY;
const float kFloatNegativeInfinity = float.NEGATIVE_INFINITY;
const float kFloatNaN = float.NAN;
const string kStringValue = "test string contents";
......@@ -31,7 +31,7 @@ def main(argv):
[
"--target 'es6'",
"--module 'es6'",
"--lib 'es6'",
"--lib 'es6, esnext.bigint'",
"--strict",
file_paths
])
......
......@@ -5,13 +5,45 @@
"""Generates Typescript source files from a mojom.Module."""
import mojom.generate.generator as generator
import mojom.generate.module as mojom
from mojom.generate.template_expander import UseJinja
_kind_to_typescript_type = {
mojom.BOOL: "boolean",
mojom.INT8: "number",
mojom.UINT8: "number",
mojom.INT16: "number",
mojom.UINT16: "number",
mojom.INT32: "number",
mojom.UINT32: "number",
mojom.FLOAT: "number",
mojom.INT64: "bigint",
mojom.UINT64: "bigint",
mojom.DOUBLE: "number",
mojom.STRING: "string",
mojom.NULLABLE_STRING: "string",
mojom.HANDLE: "MojoHandle",
mojom.DCPIPE: "MojoHandle",
mojom.DPPIPE: "MojoHandle",
mojom.MSGPIPE: "MojoHandle",
mojom.SHAREDBUFFER: "MojoHandle",
mojom.NULLABLE_HANDLE: "MojoHandle",
mojom.NULLABLE_DCPIPE: "MojoHandle",
mojom.NULLABLE_DPPIPE: "MojoHandle",
mojom.NULLABLE_MSGPIPE: "MojoHandle",
mojom.NULLABLE_SHAREDBUFFER: "MojoHandle",
}
class TypescriptStylizer(generator.Stylizer):
def StylizeModule(self, mojom_namespace):
return '.'.join(generator.ToCamel(word, lower_initial=True)
for word in mojom_namespace.split('.'))
def StylizeConstant(self, mojom_name):
# TODO(crbug.com/1009251): Constant names should be changed to match the
# style guide.
return mojom_name
class Generator(generator.Generator):
def _GetParameters(self, use_es_modules=False):
return {
......@@ -24,7 +56,10 @@ class Generator(generator.Generator):
return "ts_templates"
def GetFilters(self):
ts_filters = {}
ts_filters = {
"typescript_type_with_nullability": self._TypescriptTypeWithNullability,
"constant_value": self._ConstantValue,
}
return ts_filters
@UseJinja("mojom.tmpl")
......@@ -44,3 +79,34 @@ class Generator(generator.Generator):
self.Write(self._GenerateBindings(), "%s-lite.ts" % self.module.path)
self.Write(self._GenerateESModulesBindings(),
"%s-lite.m.ts" % self.module.path)
def _TypescriptType(self, kind):
if kind in mojom.PRIMITIVES:
return _kind_to_typescript_type[kind]
raise Exception("Type is not supported yet.")
def _TypescriptTypeWithNullability(self, kind):
return (self._TypescriptType(kind) +
(" | null" if mojom.IsNullableKind(kind) else ""))
def _ConstantValue(self, constant):
value = constant.value
if isinstance(value, (mojom.EnumValue, mojom.NamedValue)):
# TODO(crbug.com/1008761): Support EnumValue and NamedValue.
raise Exception("Constant value not supported yet. %s" % value)
if isinstance(value, mojom.BuiltinValue):
if value.value == "double.INFINITY" or value.value == "float.INFINITY":
return "Infinity";
if value.value == "double.NEGATIVE_INFINITY" or \
value.value == "float.NEGATIVE_INFINITY":
return "-Infinity";
if value.value == "double.NAN" or value.value == "float.NAN":
return "NaN";
raise Exception("Unknown BuiltinValue: %s" % value.value)
if constant.kind == mojom.INT64 or constant.kind == mojom.UINT64:
return "BigInt('%s')" % value
return value
{%- if not use_es_modules %}
namespace {{module.namespace}} {
{% endif %}
{#--- Constants #}
{%- for constant in module.constants %}
export const {{constant.name}}: {{constant.kind|typescript_type_with_nullability}} =
{{constant|constant_value}};
{%- endfor %}
{%- if not use_es_modules %}
} // namespace {{module.namespace}}
{% endif %}
......@@ -1443,6 +1443,18 @@ template("mojom") {
public_deps += [ "${full_name}_js" ]
}
}
group(js_data_deps_target_name) {
data = process_file_template(invoker.sources, js_outputs)
deps = [
":$generator_js_target_name",
]
data_deps = []
foreach(d, all_deps) {
full_name = get_label_info(d, "label_no_toolchain")
data_deps += [ "${full_name}_js_data_deps" ]
}
}
}
}
......
<!DOCTYPE html>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script src="file:///gen/layout_test_data/mojo/public/js/mojo_bindings_lite.js"></script>
<script src="file:///gen/mojo/public/js/ts/bindings/tests/constants.test-mojom-lite.js"></script>
<script>
'use strict';
[{
constant: mojo.tstest.kBoolValue,
expectedValue: true,
type: 'boolean'
}, {
constant: mojo.tstest.kInt8Value,
expectedValue: -2,
type: 'int8'
}, {
constant: mojo.tstest.kUint8Value,
expectedValue: 128,
type: 'uint8'
}, {
constant: mojo.tstest.kInt16Value,
expectedValue: -233,
type: 'int16'
}, {
constant: mojo.tstest.kUint16Value,
expectedValue: 44204,
type: 'uint16'
}, {
constant: mojo.tstest.kInt32Value,
expectedValue: -44204,
type: 'int32'
}, {
constant: mojo.tstest.kUint32Value,
expectedValue: 4294967295,
type: 'uint32'
}, {
// TODO(crbug.com/1009262): Re-enable once we generate the right value for
// 64-bit constants.
//
// constant: mojo.tstest.kInt64Value,
// expectedValue: -9223372036854775807n,
// type: 'int64'
//}, {
// constant: mojo.tstest.kUint64Value,
// expectedValue: 9999999999999999999n,
// type: 'uint64'
//}, {
constant: mojo.tstest.kDoubleValue,
expectedValue: 3.14159,
type: 'double'
}, {
constant: mojo.tstest.kDoubleInfinity,
expectedValue: Infinity,
type: 'double infinity'
}, {
constant: mojo.tstest.kDoubleNegativeInfinity,
expectedValue: -Infinity,
type: 'double negative infinity'
}, {
constant: mojo.tstest.kDoubleNaN,
expectedValue: NaN,
type: 'double NaN'
}, {
constant: mojo.tstest.kFloatValue,
expectedValue: 2.71828,
type: 'float'
}, {
constant: mojo.tstest.kFloatInfinity,
expectedValue: Infinity,
type: 'float infinity'
}, {
constant: mojo.tstest.kFloatNegativeInfinity,
expectedValue: -Infinity,
type: 'float negative infinity'
}, {
constant: mojo.tstest.kFloatNaN,
expectedValue: NaN,
type: 'NaN'
}, {
constant: mojo.tstest.kStringValue,
expectedValue: "test string contents",
type: 'string'
}].forEach(testCase => {
test(
() => assert_equals(testCase.constant, testCase.expectedValue),
`Checks that the value of generated ${testCase.type} constants is correct`);
});
</script>
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