Commit 4411204b authored by Scott Graham's avatar Scott Graham Committed by Commit Bot

fuchsia: Beginnings of FIDL JS bindings

This is the very initial stages of writing a bindings generator to allow
interop to/from JS with FIDL.

The main pieces are:
- the build time generator which consumes the JSON IR from `fidlc` and
  outputs a fidl.js
- integration with gn in fidl_library.gni
- a JS runtime support library (mostly helps with message encoding)
- new unittest binary (which also currently holds some Zircon
  integration points that will eventually be moved to a C++ runtime
  support library.)

There are many, many things it does not yet handle, e.g. responses
(either sync or async), events, passing structs, passing unions, ...

For reference, test/simple.fidl generates this
https://gist.github.com/sgraham/3935c64d5bc1b67eea1f6e3b38fef6f1


Bug: 883496
Change-Id: I58e5d2b81213f20a3198b68cbd2bc01660ba58f7
Reviewed-on: https://chromium-review.googlesource.com/c/1222697Reviewed-by: default avatarJeremy Roman <jbroman@chromium.org>
Reviewed-by: default avatarAdam Klein <adamk@chromium.org>
Reviewed-by: default avatarWez <wez@chromium.org>
Commit-Queue: Scott Graham <scottmg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#596329}
parent 0aa1d243
...@@ -216,7 +216,10 @@ group("gn_all") { ...@@ -216,7 +216,10 @@ group("gn_all") {
} else if (is_ios) { } else if (is_ios) {
deps += [ "//ios:all" ] deps += [ "//ios:all" ]
} else if (is_fuchsia) { } else if (is_fuchsia) {
deps += [ ":d8_fuchsia" ] deps += [
":d8_fuchsia",
"tools/fuchsia/fidlgen_js:fidlgen_js_unittests",
]
} }
deps += root_extra_deps deps += root_extra_deps
......
...@@ -14,12 +14,14 @@ assert(is_fuchsia) ...@@ -14,12 +14,14 @@ assert(is_fuchsia)
# namespace - (optional) Namespace for the library. # namespace - (optional) Namespace for the library.
# deps - (optional) List of other fidl_library() targets that this # deps - (optional) List of other fidl_library() targets that this
# FIDL library depends on. # FIDL library depends on.
# languages - Generate bindings for the given languages, defaults to
# [ "cpp" ]. "js" also supported.
# #
# $namespace.$library_name must match the the library name specified in the FIDL # $namespace.$library_name must match the the library name specified in the FIDL
# files. # files.
template("fidl_library") { template("fidl_library") {
forward_variables_from(invoker, [ "namespace" ]) forward_variables_from(invoker, [ "namespace", "languages" ])
_library_basename = target_name _library_basename = target_name
if (defined(invoker.library_name)) { if (defined(invoker.library_name)) {
...@@ -35,6 +37,10 @@ template("fidl_library") { ...@@ -35,6 +37,10 @@ template("fidl_library") {
_library_path = _library_basename _library_path = _library_basename
} }
if (!defined(invoker.languages)) {
languages = [ "cpp" ]
}
_response_file = "$target_gen_dir/$target_name.rsp" _response_file = "$target_gen_dir/$target_name.rsp"
_json_representation = "$target_gen_dir/${_library_name}.fidl.json" _json_representation = "$target_gen_dir/${_library_name}.fidl.json"
_output_gen_dir = "$target_gen_dir/fidl" _output_gen_dir = "$target_gen_dir/fidl"
...@@ -131,6 +137,7 @@ template("fidl_library") { ...@@ -131,6 +137,7 @@ template("fidl_library") {
action("${target_name}_cpp_gen") { action("${target_name}_cpp_gen") {
visibility = [ ":${invoker.target_name}" ] visibility = [ ":${invoker.target_name}" ]
forward_variables_from(invoker, [ "testonly" ])
deps = [ deps = [
":${invoker.target_name}_compile", ":${invoker.target_name}_compile",
...@@ -162,6 +169,40 @@ template("fidl_library") { ...@@ -162,6 +169,40 @@ template("fidl_library") {
] ]
} }
_output_js_path = "$_output_gen_dir/${_library_path}/js/fidl.js"
action("${target_name}_js_gen") {
visibility = [ ":${invoker.target_name}" ]
forward_variables_from(invoker, [ "testonly" ])
deps = [
":${invoker.target_name}_compile",
]
inputs = [
# Depend on the SDK hash, to ensure rebuild if the SDK tools change.
rebase_path("$fuchsia_sdk/.hash"),
_json_representation,
"//tools/fuchsia/fidlgen_js/fidl.py", # The schema helper file.
]
outputs = [
_output_js_path,
]
script = "//tools/fuchsia/fidlgen_js/gen.py"
args = [
rebase_path(_json_representation),
"--output",
rebase_path("${_output_js_path}"),
]
data = []
foreach(o, outputs) {
data += [ rebase_path(o) ]
}
}
config("${target_name}_config") { config("${target_name}_config") {
visibility = [ ":${invoker.target_name}" ] visibility = [ ":${invoker.target_name}" ]
include_dirs = [ _output_gen_dir ] include_dirs = [ _output_gen_dir ]
...@@ -185,10 +226,11 @@ template("fidl_library") { ...@@ -185,10 +226,11 @@ template("fidl_library") {
if (!defined(deps)) { if (!defined(deps)) {
deps = [] deps = []
} }
deps += [ deps += [ ":${invoker.target_name}_compile" ]
":${invoker.target_name}_compile",
":${invoker.target_name}_cpp_gen", foreach(language, languages) {
] deps += [ ":${invoker.target_name}_${language}_gen" ]
}
if (!defined(public_deps)) { if (!defined(public_deps)) {
public_deps = [] public_deps = []
......
# Copyright 2018 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.
import("//build/config/fuchsia/fidl_library.gni")
import("//testing/test.gni")
test("fidlgen_js_unittests") {
testonly = true
sources = [
"test/fidlgen_js_unittest.cc",
]
deps = [
":fidljstest",
":runtime",
"//base/test:test_support",
"//gin:gin_test",
"//testing/gtest",
"//v8",
]
configs += [
"//tools/v8_context_snapshot:use_v8_context_snapshot",
"//v8:external_startup_data",
]
data_deps = [
"//tools/v8_context_snapshot:v8_context_snapshot",
]
data = [
"runtime/fidl.mjs",
]
}
static_library("runtime") {
sources = [
"runtime/zircon.cc",
"runtime/zircon.h",
]
deps = [
"//gin",
"//v8",
]
}
fidl_library("fidljstest") {
testonly = true
sources = [
"test/simple.fidl",
]
languages = [
"cpp",
"js",
]
}
include_rules = [
"+gin",
"+v8/include",
]
# Copyright 2018 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.
# This was generated (and can be regenerated) by pasting
# zircon/system/host/fidl/schema.json from Fuchsia into
# https://app.quicktype.io and choosing Python 2.7 output. The only manual
# change is to modify the import path for Enum.
from third_party.enum34 import Enum
def from_str(x):
assert isinstance(x, (str, unicode))
return x
def from_int(x):
assert isinstance(x, int) and not isinstance(x, bool)
return x
def from_none(x):
assert x is None
return x
def from_union(fs, x):
for f in fs:
try:
return f(x)
except:
pass
assert False
def from_bool(x):
assert isinstance(x, bool)
return x
def to_class(c, x):
assert isinstance(x, c)
return x.to_dict()
def to_enum(c, x):
assert isinstance(x, c)
return x.value
def from_list(f, x):
assert isinstance(x, list)
return [f(y) for y in x]
def from_dict(f, x):
assert isinstance(x, dict)
return { k: f(v) for (k, v) in x.items() }
class Attribute:
def __init__(self, name, value):
self.name = name
self.value = value
@staticmethod
def from_dict(obj):
assert isinstance(obj, dict)
name = from_str(obj.get(u"name"))
value = from_str(obj.get(u"value"))
return Attribute(name, value)
def to_dict(self):
result = {}
result[u"name"] = from_str(self.name)
result[u"value"] = from_str(self.value)
return result
class TypeKind(Enum):
ARRAY = u"array"
HANDLE = u"handle"
IDENTIFIER = u"identifier"
PRIMITIVE = u"primitive"
REQUEST = u"request"
STRING = u"string"
VECTOR = u"vector"
class TypeClass:
def __init__(self, element_count, element_type, kind, maybe_element_count, nullable, subtype, identifier):
self.element_count = element_count
self.element_type = element_type
self.kind = kind
self.maybe_element_count = maybe_element_count
self.nullable = nullable
self.subtype = subtype
self.identifier = identifier
@staticmethod
def from_dict(obj):
assert isinstance(obj, dict)
element_count = from_union([from_int, from_none], obj.get(u"element_count"))
element_type = from_union([TypeClass.from_dict, from_none], obj.get(u"element_type"))
kind = TypeKind(obj.get(u"kind"))
maybe_element_count = from_union([from_int, from_none], obj.get(u"maybe_element_count"))
nullable = from_union([from_bool, from_none], obj.get(u"nullable"))
subtype = from_union([from_str, from_none], obj.get(u"subtype"))
identifier = from_union([from_str, from_none], obj.get(u"identifier"))
return TypeClass(element_count, element_type, kind, maybe_element_count, nullable, subtype, identifier)
def to_dict(self):
result = {}
result[u"element_count"] = from_union([from_int, from_none], self.element_count)
result[u"element_type"] = from_union([lambda x: to_class(TypeClass, x), from_none], self.element_type)
result[u"kind"] = to_enum(TypeKind, self.kind)
result[u"maybe_element_count"] = from_union([from_int, from_none], self.maybe_element_count)
result[u"nullable"] = from_union([from_bool, from_none], self.nullable)
result[u"subtype"] = from_union([from_str, from_none], self.subtype)
result[u"identifier"] = from_union([from_str, from_none], self.identifier)
return result
class ConstantKind(Enum):
IDENTIFIER = u"identifier"
LITERAL = u"literal"
class LiteralKind(Enum):
DEFAULT = u"default"
FALSE = u"false"
NUMERIC = u"numeric"
STRING = u"string"
TRUE = u"true"
class Literal:
def __init__(self, kind, value):
self.kind = kind
self.value = value
@staticmethod
def from_dict(obj):
assert isinstance(obj, dict)
kind = LiteralKind(obj.get(u"kind"))
value = from_union([from_str, from_none], obj.get(u"value"))
return Literal(kind, value)
def to_dict(self):
result = {}
result[u"kind"] = to_enum(LiteralKind, self.kind)
result[u"value"] = from_union([from_str, from_none], self.value)
return result
class Constant:
def __init__(self, identifier, kind, literal):
self.identifier = identifier
self.kind = kind
self.literal = literal
@staticmethod
def from_dict(obj):
assert isinstance(obj, dict)
identifier = from_union([from_str, from_none], obj.get(u"identifier"))
kind = ConstantKind(obj.get(u"kind"))
literal = from_union([Literal.from_dict, from_none], obj.get(u"literal"))
return Constant(identifier, kind, literal)
def to_dict(self):
result = {}
result[u"identifier"] = from_union([from_str, from_none], self.identifier)
result[u"kind"] = to_enum(ConstantKind, self.kind)
result[u"literal"] = from_union([lambda x: to_class(Literal, x), from_none], self.literal)
return result
class Const:
def __init__(self, maybe_attributes, name, type, value):
self.maybe_attributes = maybe_attributes
self.name = name
self.type = type
self.value = value
@staticmethod
def from_dict(obj):
assert isinstance(obj, dict)
maybe_attributes = from_union([lambda x: from_list(Attribute.from_dict, x), from_none], obj.get(u"maybe_attributes"))
name = from_str(obj.get(u"name"))
type = TypeClass.from_dict(obj.get(u"type"))
value = Constant.from_dict(obj.get(u"value"))
return Const(maybe_attributes, name, type, value)
def to_dict(self):
result = {}
result[u"maybe_attributes"] = from_union([lambda x: from_list(lambda x: to_class(Attribute, x), x), from_none], self.maybe_attributes)
result[u"name"] = from_str(self.name)
result[u"type"] = to_class(TypeClass, self.type)
result[u"value"] = to_class(Constant, self.value)
return result
class DeclarationsMap(Enum):
CONST = u"const"
ENUM = u"enum"
INTERFACE = u"interface"
STRUCT = u"struct"
UNION = u"union"
class EnumMember:
def __init__(self, name, value):
self.name = name
self.value = value
@staticmethod
def from_dict(obj):
assert isinstance(obj, dict)
name = from_str(obj.get(u"name"))
value = Constant.from_dict(obj.get(u"value"))
return EnumMember(name, value)
def to_dict(self):
result = {}
result[u"name"] = from_str(self.name)
result[u"value"] = to_class(Constant, self.value)
return result
class IntegerType(Enum):
INT16 = u"int16"
INT32 = u"int32"
INT64 = u"int64"
INT8 = u"int8"
UINT16 = u"uint16"
UINT32 = u"uint32"
UINT64 = u"uint64"
UINT8 = u"uint8"
class EnumDeclarationElement:
def __init__(self, maybe_attributes, members, name, type):
self.maybe_attributes = maybe_attributes
self.members = members
self.name = name
self.type = type
@staticmethod
def from_dict(obj):
assert isinstance(obj, dict)
maybe_attributes = from_union([lambda x: from_list(Attribute.from_dict, x), from_none], obj.get(u"maybe_attributes"))
members = from_list(EnumMember.from_dict, obj.get(u"members"))
name = from_str(obj.get(u"name"))
type = IntegerType(obj.get(u"type"))
return EnumDeclarationElement(maybe_attributes, members, name, type)
def to_dict(self):
result = {}
result[u"maybe_attributes"] = from_union([lambda x: from_list(lambda x: to_class(Attribute, x), x), from_none], self.maybe_attributes)
result[u"members"] = from_list(lambda x: to_class(EnumMember, x), self.members)
result[u"name"] = from_str(self.name)
result[u"type"] = to_enum(IntegerType, self.type)
return result
class InterfaceMethodParameter:
def __init__(self, alignment, name, offset, size, type):
self.alignment = alignment
self.name = name
self.offset = offset
self.size = size
self.type = type
@staticmethod
def from_dict(obj):
assert isinstance(obj, dict)
alignment = from_int(obj.get(u"alignment"))
name = from_str(obj.get(u"name"))
offset = from_int(obj.get(u"offset"))
size = from_int(obj.get(u"size"))
type = TypeClass.from_dict(obj.get(u"type"))
return InterfaceMethodParameter(alignment, name, offset, size, type)
def to_dict(self):
result = {}
result[u"alignment"] = from_int(self.alignment)
result[u"name"] = from_str(self.name)
result[u"offset"] = from_int(self.offset)
result[u"size"] = from_int(self.size)
result[u"type"] = to_class(TypeClass, self.type)
return result
class InterfaceMethod:
def __init__(self, has_request, has_response, maybe_attributes, maybe_request, maybe_request_alignment, maybe_request_size, maybe_response, maybe_response_alignment, maybe_response_size, name, ordinal):
self.has_request = has_request
self.has_response = has_response
self.maybe_attributes = maybe_attributes
self.maybe_request = maybe_request
self.maybe_request_alignment = maybe_request_alignment
self.maybe_request_size = maybe_request_size
self.maybe_response = maybe_response
self.maybe_response_alignment = maybe_response_alignment
self.maybe_response_size = maybe_response_size
self.name = name
self.ordinal = ordinal
@staticmethod
def from_dict(obj):
assert isinstance(obj, dict)
has_request = from_bool(obj.get(u"has_request"))
has_response = from_bool(obj.get(u"has_response"))
maybe_attributes = from_union([lambda x: from_list(Attribute.from_dict, x), from_none], obj.get(u"maybe_attributes"))
maybe_request = from_union([lambda x: from_list(InterfaceMethodParameter.from_dict, x), from_none], obj.get(u"maybe_request"))
maybe_request_alignment = from_union([from_int, from_none], obj.get(u"maybe_request_alignment"))
maybe_request_size = from_union([from_int, from_none], obj.get(u"maybe_request_size"))
maybe_response = from_union([lambda x: from_list(InterfaceMethodParameter.from_dict, x), from_none], obj.get(u"maybe_response"))
maybe_response_alignment = from_union([from_int, from_none], obj.get(u"maybe_response_alignment"))
maybe_response_size = from_union([from_int, from_none], obj.get(u"maybe_response_size"))
name = from_str(obj.get(u"name"))
ordinal = from_int(obj.get(u"ordinal"))
return InterfaceMethod(has_request, has_response, maybe_attributes, maybe_request, maybe_request_alignment, maybe_request_size, maybe_response, maybe_response_alignment, maybe_response_size, name, ordinal)
def to_dict(self):
result = {}
result[u"has_request"] = from_bool(self.has_request)
result[u"has_response"] = from_bool(self.has_response)
result[u"maybe_attributes"] = from_union([lambda x: from_list(lambda x: to_class(Attribute, x), x), from_none], self.maybe_attributes)
result[u"maybe_request"] = from_union([lambda x: from_list(lambda x: to_class(InterfaceMethodParameter, x), x), from_none], self.maybe_request)
result[u"maybe_request_alignment"] = from_union([from_int, from_none], self.maybe_request_alignment)
result[u"maybe_request_size"] = from_union([from_int, from_none], self.maybe_request_size)
result[u"maybe_response"] = from_union([lambda x: from_list(lambda x: to_class(InterfaceMethodParameter, x), x), from_none], self.maybe_response)
result[u"maybe_response_alignment"] = from_union([from_int, from_none], self.maybe_response_alignment)
result[u"maybe_response_size"] = from_union([from_int, from_none], self.maybe_response_size)
result[u"name"] = from_str(self.name)
result[u"ordinal"] = from_int(self.ordinal)
return result
class Interface:
def __init__(self, maybe_attributes, methods, name):
self.maybe_attributes = maybe_attributes
self.methods = methods
self.name = name
@staticmethod
def from_dict(obj):
assert isinstance(obj, dict)
maybe_attributes = from_union([lambda x: from_list(Attribute.from_dict, x), from_none], obj.get(u"maybe_attributes"))
methods = from_list(InterfaceMethod.from_dict, obj.get(u"methods"))
name = from_str(obj.get(u"name"))
return Interface(maybe_attributes, methods, name)
def to_dict(self):
result = {}
result[u"maybe_attributes"] = from_union([lambda x: from_list(lambda x: to_class(Attribute, x), x), from_none], self.maybe_attributes)
result[u"methods"] = from_list(lambda x: to_class(InterfaceMethod, x), self.methods)
result[u"name"] = from_str(self.name)
return result
class Library:
def __init__(self, declarations, name):
self.declarations = declarations
self.name = name
@staticmethod
def from_dict(obj):
assert isinstance(obj, dict)
declarations = from_dict(DeclarationsMap, obj.get(u"declarations"))
name = from_str(obj.get(u"name"))
return Library(declarations, name)
def to_dict(self):
result = {}
result[u"declarations"] = from_dict(lambda x: to_enum(DeclarationsMap, x), self.declarations)
result[u"name"] = from_str(self.name)
return result
class StructMember:
def __init__(self, alignment, maybe_default_value, name, offset, size, type):
self.alignment = alignment
self.maybe_default_value = maybe_default_value
self.name = name
self.offset = offset
self.size = size
self.type = type
@staticmethod
def from_dict(obj):
assert isinstance(obj, dict)
alignment = from_int(obj.get(u"alignment"))
maybe_default_value = from_union([Constant.from_dict, from_none], obj.get(u"maybe_default_value"))
name = from_str(obj.get(u"name"))
offset = from_int(obj.get(u"offset"))
size = from_int(obj.get(u"size"))
type = TypeClass.from_dict(obj.get(u"type"))
return StructMember(alignment, maybe_default_value, name, offset, size, type)
def to_dict(self):
result = {}
result[u"alignment"] = from_int(self.alignment)
result[u"maybe_default_value"] = from_union([lambda x: to_class(Constant, x), from_none], self.maybe_default_value)
result[u"name"] = from_str(self.name)
result[u"offset"] = from_int(self.offset)
result[u"size"] = from_int(self.size)
result[u"type"] = to_class(TypeClass, self.type)
return result
class Struct:
def __init__(self, max_handles, maybe_attributes, members, name, size):
self.max_handles = max_handles
self.maybe_attributes = maybe_attributes
self.members = members
self.name = name
self.size = size
@staticmethod
def from_dict(obj):
assert isinstance(obj, dict)
max_handles = from_union([from_int, from_none], obj.get(u"max_handles"))
maybe_attributes = from_union([lambda x: from_list(Attribute.from_dict, x), from_none], obj.get(u"maybe_attributes"))
members = from_list(StructMember.from_dict, obj.get(u"members"))
name = from_str(obj.get(u"name"))
size = from_int(obj.get(u"size"))
return Struct(max_handles, maybe_attributes, members, name, size)
def to_dict(self):
result = {}
result[u"max_handles"] = from_union([from_int, from_none], self.max_handles)
result[u"maybe_attributes"] = from_union([lambda x: from_list(lambda x: to_class(Attribute, x), x), from_none], self.maybe_attributes)
result[u"members"] = from_list(lambda x: to_class(StructMember, x), self.members)
result[u"name"] = from_str(self.name)
result[u"size"] = from_int(self.size)
return result
class UnionMember:
def __init__(self, alignment, name, offset, size, type):
self.alignment = alignment
self.name = name
self.offset = offset
self.size = size
self.type = type
@staticmethod
def from_dict(obj):
assert isinstance(obj, dict)
alignment = from_int(obj.get(u"alignment"))
name = from_str(obj.get(u"name"))
offset = from_int(obj.get(u"offset"))
size = from_int(obj.get(u"size"))
type = TypeClass.from_dict(obj.get(u"type"))
return UnionMember(alignment, name, offset, size, type)
def to_dict(self):
result = {}
result[u"alignment"] = from_int(self.alignment)
result[u"name"] = from_str(self.name)
result[u"offset"] = from_int(self.offset)
result[u"size"] = from_int(self.size)
result[u"type"] = to_class(TypeClass, self.type)
return result
class UnionDeclarationElement:
def __init__(self, alignment, max_handles, maybe_attributes, members, name, size):
self.alignment = alignment
self.max_handles = max_handles
self.maybe_attributes = maybe_attributes
self.members = members
self.name = name
self.size = size
@staticmethod
def from_dict(obj):
assert isinstance(obj, dict)
alignment = from_int(obj.get(u"alignment"))
max_handles = from_union([from_int, from_none], obj.get(u"max_handles"))
maybe_attributes = from_union([lambda x: from_list(Attribute.from_dict, x), from_none], obj.get(u"maybe_attributes"))
members = from_list(UnionMember.from_dict, obj.get(u"members"))
name = from_str(obj.get(u"name"))
size = from_int(obj.get(u"size"))
return UnionDeclarationElement(alignment, max_handles, maybe_attributes, members, name, size)
def to_dict(self):
result = {}
result[u"alignment"] = from_int(self.alignment)
result[u"max_handles"] = from_union([from_int, from_none], self.max_handles)
result[u"maybe_attributes"] = from_union([lambda x: from_list(lambda x: to_class(Attribute, x), x), from_none], self.maybe_attributes)
result[u"members"] = from_list(lambda x: to_class(UnionMember, x), self.members)
result[u"name"] = from_str(self.name)
result[u"size"] = from_int(self.size)
return result
class Fidl:
def __init__(self, const_declarations, declaration_order, declarations, enum_declarations, interface_declarations, library_dependencies, name, struct_declarations, union_declarations, version):
self.const_declarations = const_declarations
self.declaration_order = declaration_order
self.declarations = declarations
self.enum_declarations = enum_declarations
self.interface_declarations = interface_declarations
self.library_dependencies = library_dependencies
self.name = name
self.struct_declarations = struct_declarations
self.union_declarations = union_declarations
self.version = version
@staticmethod
def from_dict(obj):
assert isinstance(obj, dict)
const_declarations = from_list(Const.from_dict, obj.get(u"const_declarations"))
declaration_order = from_list(from_str, obj.get(u"declaration_order"))
declarations = from_dict(DeclarationsMap, obj.get(u"declarations"))
enum_declarations = from_list(EnumDeclarationElement.from_dict, obj.get(u"enum_declarations"))
interface_declarations = from_list(Interface.from_dict, obj.get(u"interface_declarations"))
library_dependencies = from_list(Library.from_dict, obj.get(u"library_dependencies"))
name = from_str(obj.get(u"name"))
struct_declarations = from_list(Struct.from_dict, obj.get(u"struct_declarations"))
union_declarations = from_list(UnionDeclarationElement.from_dict, obj.get(u"union_declarations"))
version = from_str(obj.get(u"version"))
return Fidl(const_declarations, declaration_order, declarations, enum_declarations, interface_declarations, library_dependencies, name, struct_declarations, union_declarations, version)
def to_dict(self):
result = {}
result[u"const_declarations"] = from_list(lambda x: to_class(Const, x), self.const_declarations)
result[u"declaration_order"] = from_list(from_str, self.declaration_order)
result[u"declarations"] = from_dict(lambda x: to_enum(DeclarationsMap, x), self.declarations)
result[u"enum_declarations"] = from_list(lambda x: to_class(EnumDeclarationElement, x), self.enum_declarations)
result[u"interface_declarations"] = from_list(lambda x: to_class(Interface, x), self.interface_declarations)
result[u"library_dependencies"] = from_list(lambda x: to_class(Library, x), self.library_dependencies)
result[u"name"] = from_str(self.name)
result[u"struct_declarations"] = from_list(lambda x: to_class(Struct, x), self.struct_declarations)
result[u"union_declarations"] = from_list(lambda x: to_class(UnionDeclarationElement, x), self.union_declarations)
result[u"version"] = from_str(self.version)
return result
def fidl_from_dict(s):
return Fidl.from_dict(s)
def fidl_to_dict(x):
return to_class(Fidl, x)
#!/usr/bin/env python
# Copyright 2018 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.
import argparse
from fidl import *
import json
class _CompoundIdentifier(object):
def __init__(self, library, name):
self.library = library
self.name = name
def _ParseLibraryName(lib):
return lib.split('.')
def _ParseCompoundIdentifier(ident):
parts = ident.split('/', 2)
raw_library = ''
raw_name = parts[0]
if len(parts) == 2:
raw_library, raw_name = parts
library = _ParseLibraryName(raw_library)
return _CompoundIdentifier(library, raw_name)
def _ChangeIfReserved(name):
# TODO(crbug.com/883496): Remap any JS keywords.
return name
def _CompileCompoundIdentifier(compound, ext=''):
result = _ChangeIfReserved(compound.name) + ext
return result
def _CompileIdentifier(ident):
return _ChangeIfReserved(ident)
def _JsTypeForPrimitiveType(t):
mapping = {
IntegerType.INT16: 'number',
IntegerType.INT32: 'number',
IntegerType.INT64: 'BigInt',
IntegerType.INT8: 'number',
IntegerType.UINT16: 'number',
IntegerType.UINT32: 'number',
IntegerType.UINT64: 'BigInt',
IntegerType.UINT8: 'number',
}
return mapping[t]
def _InlineSizeOfType(t):
if t.kind == TypeKind.PRIMITIVE:
return {
'int16': 2,
'int32': 4,
'int64': 8,
'int8': 1,
'uint16': 2,
'uint32': 4,
'uint64': 8,
'uint8': 1,
}[t.subtype]
else:
raise NotImplementedError()
def _CompileConstant(val):
if val.kind == ConstantKind.IDENTIFIER:
raise NotImplementedError()
elif val.kind == ConstantKind.LITERAL:
return _CompileLiteral(val.literal)
else:
raise Exception('unexpected kind')
def _CompileLiteral(val):
if val.kind == LiteralKind.STRING:
# TODO(crbug.com/883496): This needs to encode the string in an escaped
# form suitable to JS. Currently using the escaped Python representation,
# which is passably compatible, but surely has differences in edge cases.
return repr(val.value)
elif val.kind == LiteralKind.NUMERIC:
return val.value
elif val.kind == LiteralKind.TRUE:
return 'true'
elif val.kind == LiteralKind.FALSE:
return 'false'
elif val.kind == LiteralKind.DEFAULT:
return 'default'
else:
raise Exception('unexpected kind')
class Compiler(object):
def __init__(self, fidl, output_file):
self.fidl = fidl
self.f = output_file
def Compile(self):
self._EmitHeader()
for c in self.fidl.const_declarations:
self._CompileConst(c)
for e in self.fidl.enum_declarations:
self._CompileEnum(e)
if self.fidl.union_declarations:
raise NotImplementedError()
if self.fidl.struct_declarations:
raise NotImplementedError()
for i in self.fidl.interface_declarations:
self._CompileInterface(i)
def _EmitHeader(self):
self.f.write('''// Copyright 2018 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.
//
// WARNING: This file is machine generated by fidlgen_js.
''')
def _CompileConst(self, c):
self.f.write('''/**
* @const {%(type)s}
*/
const %(name)s = %(value)s;
''' % c.to_dict())
def _CompileEnum(self, enum):
compound = _ParseCompoundIdentifier(enum.name)
name = _CompileCompoundIdentifier(compound)
js_type = _JsTypeForPrimitiveType(enum.type)
data = { 'js_type': js_type, 'type': enum.type.value, 'name': name }
self.f.write('''/**
* @enum {%(js_type)s}
*/
const %(name)s = {
''' % data)
for member in enum.members:
self.f.write(''' %s: %s,\n''' %
(member.name, _CompileConstant(member.value)))
self.f.write('};\n')
self.f.write('const _kTT_%(name)s = _kTT_%(type)s;\n\n' % data)
def _CompileType(self, t):
if t.kind == TypeKind.PRIMITIVE:
return t.subtype
elif t.kind == TypeKind.STRING:
return 'String' + ('_Nullable' if t.nullable else '_Nonnull')
elif t.kind == TypeKind.IDENTIFIER:
compound = _ParseCompoundIdentifier(t.identifier)
name = _CompileCompoundIdentifier(compound)
return name
elif t.kind == TypeKind.VECTOR:
element_ttname = self._CompileType(t.element_type)
ttname = ('VEC_' + ('Nullable_' if t.nullable else 'Nonnull_') +
element_ttname)
throw_if_null = '/* v may be null */'
pointer_set = ''' if (v === null || v === undefined) {
e.data.setUint32(o + 8, 0, $fidl__kLE);
e.data.setUint32(o + 12, 0, $fidl__kLE);
} else {
e.data.setUint32(o + 8, 0xffffffff, $fidl__kLE);
e.data.setUint32(o + 12, 0xffffffff, $fidl__kLE);
}'''
if not t.nullable:
throw_if_null = ('if (v === null || v === undefined) '
'throw "non-null vector required";')
pointer_set = ''' e.data.setUint32(o + 8, 0xffffffff, $fidl__kLE);
e.data.setUint32(o + 12, 0xffffffff, $fidl__kLE);'''
self.f.write(
'''const _kTT_%(ttname)s = {
enc: function(e, o, v) {
%(throw_if_null)s
e.data.setUint32(o, v.length, $fidl__kLE);
e.data.setUint32(o + 4, 0, $fidl__kLE);
%(pointer_set)s
e.outOfLine.push([function(e, body) {
var start = e.alloc(body.length * %(element_size)s);
for (var i = 0; i < body.length; i++) {
_kTT_%(element_ttname)s.enc(e, start + (i * %(element_size)s), body[i]);
}
},
v]);
},
};
''' % { 'ttname': ttname,
'element_ttname': element_ttname,
'element_size': _InlineSizeOfType(t.element_type),
'pointer_set': pointer_set,
'throw_if_null': throw_if_null })
return ttname
else:
raise NotImplementedError()
def _GenerateJsInterfaceForInterface(self, name, interface):
"""Generates a JS @interface for the given FIDL interface."""
self.f.write('''/**
* @interface
*/
function %(name)s() {}
''' % { 'name': name })
# Define a JS interface part for the interface for typechecking.
for method in interface.methods:
method_name = _CompileIdentifier(method.name)
if method.has_request:
param_names = [_CompileIdentifier(x.name)
for x in method.maybe_request]
if len(param_names):
self.f.write('/**\n')
# TODO(crbug.com/883496): Emit @param type comments here.
self.f.write(' */\n')
self.f.write('%(name)s.prototype.%(method_name)s = '
'function(%(param_names)s) {};\n\n' % {
'name': name,
'method_name': method_name,
'param_names': ', '.join(param_names)})
# Emit message ordinals for later use.
for method in interface.methods:
method_name = _CompileIdentifier(method.name)
self.f.write(
'const _k%(name)s_%(method_name)s_Ordinal = %(ordinal)s;\n' % {
'name': name,
'method_name': method_name,
'ordinal': method.ordinal})
self.f.write('\n')
def _GenerateJsProxyForInterface(self, name, interface):
"""Generates the JS side implementation of a proxy class implementing the
given interface."""
proxy_name = name + 'Proxy'
self.f.write('''/**
* @constructor
* @implements %(name)s
*/
function %(proxy_name)s() {
this.channel = zx.ZX_HANDLE_INVALID;
}
%(proxy_name)s.prototype.$bind = function(channel) {
this.channel = channel;
};
''' % { 'name': name,
'proxy_name': proxy_name })
for method in interface.methods:
method_name = _CompileIdentifier(method.name)
if method.has_request:
type_tables = []
for param in method.maybe_request:
type_tables.append(self._CompileType(param.type))
param_names = [_CompileIdentifier(x.name) for x in method.maybe_request]
self.f.write(
'''%(proxy_name)s.prototype.%(method_name)s = function(%(param_names)s) {
if (this.channel === zx.ZX_HANDLE_INVALID) {
throw "channel closed";
}
var $encoder = new $fidl_Encoder(_k%(name)s_%(method_name)s_Ordinal);
$encoder.alloc(%(size)s - $fidl_kMessageHeaderSize);
''' % { 'name': name,
'proxy_name': proxy_name,
'method_name': method_name,
'param_names': ', '.join(param_names),
'size': method.maybe_request_size})
for param, ttname in zip(method.maybe_request, type_tables):
self.f.write(
''' _kTT_%(type_table)s.enc($encoder, %(offset)s, %(param_name)s);
''' % { 'type_table': ttname,
'param_name': _CompileIdentifier(param.name),
'offset': param.offset })
self.f.write(
''' var $result = zx.channelWrite(this.channel,
$encoder.messageData(),
$encoder.messageHandles());
if ($result !== zx.ZX_OK) {
throw "zx.channelWrite failed: " + $result;
}
};
''')
def _CompileInterface(self, interface):
compound = _ParseCompoundIdentifier(interface.name)
name = _CompileCompoundIdentifier(compound)
self._GenerateJsInterfaceForInterface(name, interface)
self._GenerateJsProxyForInterface(name, interface)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('json')
parser.add_argument('--output', required=True)
args = parser.parse_args()
fidl = fidl_from_dict(json.load(open(args.json, 'r')))
with open(args.output, 'w') as f:
c = Compiler(fidl, f)
c.Compile()
if __name__ == '__main__':
main()
// Copyright 2018 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.
// This is the JS runtime support library for code generated by fidlgen_js. It
// mostly consists of helpers to facilitate encoding and decoding of FIDL
// messages.
const $fidl_kInitialBufferSize = 1024;
const $fidl_kMessageHeaderSize = 16;
const $fidl_kMessageTxidOffset = 0;
const $fidl_kMessageOrdinalOffset = 12;
const $fidl__kAlignment = 8;
const $fidl__kAlignmentMask = 0x7;
const $fidl__kLE = true;
function $fidl__align(size) {
return size + (($fidl__kAlignment - (size & $fidl__kAlignmentMask)) &
$fidl__kAlignmentMask);
}
/**
* @constructor
* @param {number} ordinal
*/
function $fidl_Encoder(ordinal) {
var buf = new ArrayBuffer($fidl_kInitialBufferSize);
this.data = new DataView(buf);
this.extent = 0;
this.handles = [];
this.outOfLine = [];
this._encodeMessageHeader(ordinal);
}
/**
* @param {number} ordinal
*/
$fidl_Encoder.prototype._encodeMessageHeader = function(ordinal) {
this.alloc($fidl_kMessageHeaderSize);
this.data.setUint32($fidl_kMessageOrdinalOffset, ordinal, $fidl__kLE);
};
/**
* @param {number} size
*/
$fidl_Encoder.prototype.alloc = function(size) {
var offset = this.extent;
this._claimMemory($fidl__align(size));
return offset;
};
/**
* @param {number} claimSize
*/
$fidl_Encoder.prototype._claimMemory = function(claimSize) {
this.extent += claimSize;
if (this.extent > this.data.byteLength) {
var newSize = this.data.byteLength + claimSize;
newSize += newSize * 2;
this._grow(newSize);
}
};
/**
* @param {number} newSize
*/
$fidl_Encoder.prototype._grow = function(newSize) {
var newBuffer = new ArrayBuffer(newSize);
new Uint8Array(newBuffer).set(new Uint8Array(this.data.buffer));
this.data = new DataView(newBuffer);
};
$fidl_Encoder.prototype.messageData = function() {
// Add all out of line data.
var len = this.outOfLine.length;
for (var i = 0; i < len; i++) {
this.outOfLine[i][0](this, this.outOfLine[i][1]);
}
// Return final result.
return new DataView(this.data.buffer, 0, this.extent);
};
$fidl_Encoder.prototype.messageHandles = function() {
return this.handles;
};
// Type tables and encoding helpers for generated Proxy code.
const _kTT_int8 = {
enc: function(e, o, v) { e.data.setInt8(o, v, $fidl__kLE); },
};
const _kTT_int16 = {
enc: function(e, o, v) { e.data.setInt16(o, v, $fidl__kLE); },
};
const _kTT_int32 = {
enc: function(e, o, v) { e.data.setUint32(o, v, $fidl__kLE); },
};
const _kTT_uint8 = {
enc: function(e, o, v) { e.data.setUint8(o, v, $fidl__kLE); },
};
const _kTT_uint16 = {
enc: function(e, o, v) { e.data.setUint16(o, v, $fidl__kLE); },
};
const _kTT_uint32 = {
enc: function(e, o, v) { e.data.setUint32(o, v, $fidl__kLE); },
};
const _kTT_String_Nonnull = {
enc: function(e, o, v) {
if (v === null || v === undefined) throw "non-null string required";
// Both size and data are uint64, but that's awkward in JS, so for now only
// support a maximum of 32b lengths.
var asUtf8 = zx.strToUtf8Array(v);
e.data.setUint32(o, asUtf8.length, $fidl__kLE);
e.data.setUint32(o + 4, 0, $fidl__kLE);
e.data.setUint32(o + 8, 0xffffffff, $fidl__kLE);
e.data.setUint32(o + 12, 0xffffffff, $fidl__kLE);
e.outOfLine.push([$fidl_OutOfLineStringEnc, asUtf8]);
},
};
function $fidl_OutOfLineStringEnc(e, strAsUtf8Array) {
var start = e.alloc(strAsUtf8Array.length);
for (var i = 0; i < strAsUtf8Array.length; i++) {
e.data.setUint8(start + i, strAsUtf8Array[i], $fidl__kLE);
}
}
// Copyright 2018 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.
#include "tools/fuchsia/fidlgen_js/runtime/zircon.h"
#include <lib/zx/channel.h>
#include <zircon/errors.h>
#include <zircon/syscalls.h>
#include <zircon/types.h>
#include "base/bind.h"
#include "gin/arguments.h"
#include "gin/array_buffer.h"
#include "gin/converter.h"
#include "gin/data_object_builder.h"
#include "gin/function_template.h"
namespace {
v8::Local<v8::Object> ZxChannelCreate(v8::Isolate* isolate) {
zx::channel c1, c2;
zx_status_t status = zx::channel::create(0, &c1, &c2);
return gin::DataObjectBuilder(isolate)
.Set("status", status)
.Set("first", c1.release())
.Set("second", c2.release())
.Build();
}
zx_status_t ZxChannelWrite(gin::Arguments* args) {
zx_handle_t handle;
if (!args->GetNext(&handle)) {
args->ThrowError();
return ZX_ERR_INVALID_ARGS;
}
gin::ArrayBufferView data;
if (!args->GetNext(&data)) {
args->ThrowError();
return ZX_ERR_INVALID_ARGS;
}
std::vector<zx_handle_t> handles;
if (!args->GetNext(&handles)) {
args->ThrowError();
return ZX_ERR_INVALID_ARGS;
}
zx_status_t status =
zx_channel_write(handle, 0, data.bytes(), data.num_bytes(),
handles.data(), handles.size());
return status;
}
v8::Local<v8::Value> StrToUtf8Array(gin::Arguments* args) {
std::string str;
// This converts the string to utf8 from ucs2, so then just repackage the
// string as an array and return it.
if (!args->GetNext(&str)) {
args->ThrowError();
return v8::Local<v8::Object>();
}
// TODO(crbug.com/883496): Not sure how to make a Uint8Array to return here
// which would be a bit more efficient.
std::vector<int> data;
std::copy(str.begin(), str.end(), std::back_inserter(data));
return gin::ConvertToV8(args->isolate(), data);
}
v8::Local<v8::Object> GetOrCreateZxObject(v8::Isolate* isolate,
v8::Local<v8::Object> global) {
v8::Local<v8::Object> zx;
v8::Local<v8::Value> zx_value = global->Get(gin::StringToV8(isolate, "zx"));
if (zx_value.IsEmpty() || !zx_value->IsObject()) {
zx = v8::Object::New(isolate);
global->Set(gin::StringToSymbol(isolate, "zx"), zx);
} else {
zx = v8::Local<v8::Object>::Cast(zx);
}
return zx;
}
} // namespace
namespace fidljs {
void InjectZxBindings(v8::Isolate* isolate, v8::Local<v8::Object> global) {
v8::Local<v8::Object> zx = GetOrCreateZxObject(isolate, global);
#define SET_CONSTANT(k) \
zx->Set(gin::StringToSymbol(isolate, #k), gin::ConvertToV8(isolate, k))
// zx_status_t.
SET_CONSTANT(ZX_OK);
SET_CONSTANT(ZX_ERR_INTERNAL);
SET_CONSTANT(ZX_ERR_NOT_SUPPORTED);
SET_CONSTANT(ZX_ERR_NO_RESOURCES);
SET_CONSTANT(ZX_ERR_NO_MEMORY);
SET_CONSTANT(ZX_ERR_INTERNAL_INTR_RETRY);
SET_CONSTANT(ZX_ERR_INVALID_ARGS);
SET_CONSTANT(ZX_ERR_BAD_HANDLE);
SET_CONSTANT(ZX_ERR_WRONG_TYPE);
SET_CONSTANT(ZX_ERR_BAD_SYSCALL);
SET_CONSTANT(ZX_ERR_OUT_OF_RANGE);
SET_CONSTANT(ZX_ERR_BUFFER_TOO_SMALL);
SET_CONSTANT(ZX_ERR_BAD_STATE);
SET_CONSTANT(ZX_ERR_TIMED_OUT);
SET_CONSTANT(ZX_ERR_SHOULD_WAIT);
SET_CONSTANT(ZX_ERR_CANCELED);
SET_CONSTANT(ZX_ERR_PEER_CLOSED);
SET_CONSTANT(ZX_ERR_NOT_FOUND);
SET_CONSTANT(ZX_ERR_ALREADY_EXISTS);
SET_CONSTANT(ZX_ERR_ALREADY_BOUND);
SET_CONSTANT(ZX_ERR_UNAVAILABLE);
SET_CONSTANT(ZX_ERR_ACCESS_DENIED);
SET_CONSTANT(ZX_ERR_IO);
SET_CONSTANT(ZX_ERR_IO_REFUSED);
SET_CONSTANT(ZX_ERR_IO_DATA_INTEGRITY);
SET_CONSTANT(ZX_ERR_IO_DATA_LOSS);
SET_CONSTANT(ZX_ERR_IO_NOT_PRESENT);
SET_CONSTANT(ZX_ERR_IO_OVERRUN);
SET_CONSTANT(ZX_ERR_IO_MISSED_DEADLINE);
SET_CONSTANT(ZX_ERR_IO_INVALID);
SET_CONSTANT(ZX_ERR_BAD_PATH);
SET_CONSTANT(ZX_ERR_NOT_DIR);
SET_CONSTANT(ZX_ERR_NOT_FILE);
SET_CONSTANT(ZX_ERR_FILE_BIG);
SET_CONSTANT(ZX_ERR_NO_SPACE);
SET_CONSTANT(ZX_ERR_NOT_EMPTY);
SET_CONSTANT(ZX_ERR_STOP);
SET_CONSTANT(ZX_ERR_NEXT);
SET_CONSTANT(ZX_ERR_ASYNC);
SET_CONSTANT(ZX_ERR_PROTOCOL_NOT_SUPPORTED);
SET_CONSTANT(ZX_ERR_ADDRESS_UNREACHABLE);
SET_CONSTANT(ZX_ERR_ADDRESS_IN_USE);
SET_CONSTANT(ZX_ERR_NOT_CONNECTED);
SET_CONSTANT(ZX_ERR_CONNECTION_REFUSED);
SET_CONSTANT(ZX_ERR_CONNECTION_RESET);
SET_CONSTANT(ZX_ERR_CONNECTION_ABORTED);
// Handle APIs.
zx->Set(gin::StringToSymbol(isolate, "handleClose"),
gin::CreateFunctionTemplate(isolate,
base::BindRepeating(&zx_handle_close))
->GetFunction());
SET_CONSTANT(ZX_HANDLE_INVALID);
// Channel APIs.
zx->Set(gin::StringToSymbol(isolate, "channelCreate"),
gin::CreateFunctionTemplate(isolate,
base::BindRepeating(&ZxChannelCreate))
->GetFunction());
zx->Set(
gin::StringToSymbol(isolate, "channelWrite"),
gin::CreateFunctionTemplate(isolate, base::BindRepeating(&ZxChannelWrite))
->GetFunction());
SET_CONSTANT(ZX_CHANNEL_READABLE);
SET_CONSTANT(ZX_CHANNEL_WRITABLE);
SET_CONSTANT(ZX_CHANNEL_PEER_CLOSED);
SET_CONSTANT(ZX_CHANNEL_READ_MAY_DISCARD);
SET_CONSTANT(ZX_CHANNEL_MAX_MSG_BYTES);
SET_CONSTANT(ZX_CHANNEL_MAX_MSG_HANDLES);
// Utility to make string handling easier to convert from a UCS2 JS string to
// an array of UTF-8 (which is how strings are represented in FIDL).
// TODO(crbug.com/883496): This is not really zx, should move to a generic
// runtime helper file if there are more similar C++ helpers required.
zx->Set(
gin::StringToSymbol(isolate, "strToUtf8Array"),
gin::CreateFunctionTemplate(isolate, base::BindRepeating(&StrToUtf8Array))
->GetFunction());
#undef SET_CONSTANT
}
} // namespace fidljs
// Copyright 2018 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.
#ifndef TOOLS_FUCHSIA_FIDLGEN_JS_RUNTIME_ZIRCON_H_
#define TOOLS_FUCHSIA_FIDLGEN_JS_RUNTIME_ZIRCON_H_
#include "v8/include/v8.h"
namespace fidljs {
// Adds Zircon APIs bindings to |global|, for use by JavaScript callers.
void InjectZxBindings(v8::Isolate* isolate, v8::Local<v8::Object> global);
} // namespace fidljs
#endif // TOOLS_FUCHSIA_FIDLGEN_JS_RUNTIME_ZIRCON_H_
// Copyright 2018 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.
#include "base/bind.h"
#include "base/stl_util.h"
#include "base/strings/stringprintf.h"
#include "base/test/launcher/unit_test_launcher.h"
#include "base/test/test_suite.h"
#include "gin/converter.h"
#include "gin/modules/console.h"
#include "gin/object_template_builder.h"
#include "gin/public/isolate_holder.h"
#include "gin/shell_runner.h"
#include "gin/test/v8_test.h"
#include "gin/try_catch.h"
#include "tools/fuchsia/fidlgen_js/fidl/fidljstest/cpp/fidl.h"
#include "tools/fuchsia/fidlgen_js/runtime/zircon.h"
#include "v8/include/v8.h"
static const char kRuntimeFile[] =
"/pkg/tools/fuchsia/fidlgen_js/runtime/fidl.mjs";
static const char kTestBindingFile[] =
"/pkg/tools/fuchsia/fidlgen_js/fidl/fidljstest/js/fidl.js";
class FidlGenJsTestShellRunnerDelegate : public gin::ShellRunnerDelegate {
public:
FidlGenJsTestShellRunnerDelegate() {}
v8::Local<v8::ObjectTemplate> GetGlobalTemplate(
gin::ShellRunner* runner,
v8::Isolate* isolate) override {
v8::Local<v8::ObjectTemplate> templ =
gin::ObjectTemplateBuilder(isolate).Build();
gin::Console::Register(isolate, templ);
return templ;
}
void UnhandledException(gin::ShellRunner* runner,
gin::TryCatch& try_catch) override {
LOG(ERROR) << try_catch.GetStackTrace();
ADD_FAILURE();
}
private:
DISALLOW_COPY_AND_ASSIGN(FidlGenJsTestShellRunnerDelegate);
};
using FidlGenJsTest = gin::V8Test;
TEST_F(FidlGenJsTest, BasicJSSetup) {
v8::Isolate* isolate = instance_->isolate();
std::string source = "log('this is a log'); this.stuff = 'HAI';";
FidlGenJsTestShellRunnerDelegate delegate;
gin::ShellRunner runner(&delegate, isolate);
gin::Runner::Scope scope(&runner);
runner.Run(source, "test.js");
std::string result;
EXPECT_TRUE(gin::Converter<std::string>::FromV8(
isolate, runner.global()->Get(gin::StringToV8(isolate, "stuff")),
&result));
EXPECT_EQ("HAI", result);
}
TEST_F(FidlGenJsTest, CreateChannelPair) {
v8::Isolate* isolate = instance_->isolate();
v8::HandleScope handle_scope(isolate);
FidlGenJsTestShellRunnerDelegate delegate;
gin::ShellRunner runner(&delegate, isolate);
gin::Runner::Scope scope(&runner);
fidljs::InjectZxBindings(isolate, runner.global());
std::string source = R"(
var result = zx.channelCreate();
this.result_status = result.status;
this.result_h1 = result.first;
this.result_h2 = result.second;
if (result.status == zx.ZX_OK) {
zx.handleClose(result.first);
zx.handleClose(result.second);
}
)";
runner.Run(source, "test.js");
zx_status_t status = ZX_ERR_INTERNAL;
EXPECT_TRUE(gin::Converter<zx_status_t>::FromV8(
isolate, runner.global()->Get(gin::StringToV8(isolate, "result_status")),
&status));
EXPECT_EQ(status, ZX_OK);
zx_handle_t handle = ZX_HANDLE_INVALID;
EXPECT_TRUE(gin::Converter<zx_handle_t>::FromV8(
isolate, runner.global()->Get(gin::StringToV8(isolate, "result_h1")),
&handle));
EXPECT_NE(handle, ZX_HANDLE_INVALID);
EXPECT_TRUE(gin::Converter<zx_handle_t>::FromV8(
isolate, runner.global()->Get(gin::StringToV8(isolate, "result_h2")),
&handle));
EXPECT_NE(handle, ZX_HANDLE_INVALID);
}
class TestolaImpl : public fidljstest::Testola {
public:
TestolaImpl() = default;
~TestolaImpl() override {}
void DoSomething() override { was_do_something_called_ = true; }
void PrintInt(int32_t number) override { received_int_ = number; }
void PrintMsg(fidl::StringPtr message) override {
std::string as_str = message.get();
received_msg_ = as_str;
}
void VariousArgs(fidljstest::Blorp blorp,
fidl::StringPtr msg,
fidl::VectorPtr<uint32_t> stuff) override {
std::string msg_as_str = msg.get();
std::vector<uint32_t> stuff_as_vec = stuff.get();
various_blorp_ = blorp;
various_msg_ = msg_as_str;
various_stuff_ = stuff_as_vec;
}
bool was_do_something_called() const { return was_do_something_called_; }
int32_t received_int() const { return received_int_; }
const std::string& received_msg() const { return received_msg_; }
fidljstest::Blorp various_blorp() const { return various_blorp_; }
const std::string& various_msg() const { return various_msg_; }
const std::vector<uint32_t>& various_stuff() const { return various_stuff_; }
private:
bool was_do_something_called_ = false;
int32_t received_int_ = -1;
std::string received_msg_;
fidljstest::Blorp various_blorp_;
std::string various_msg_;
std::vector<uint32_t> various_stuff_;
DISALLOW_COPY_AND_ASSIGN(TestolaImpl);
};
void LoadAndSource(gin::ShellRunner* runner, const base::FilePath& filename) {
std::string contents;
ASSERT_TRUE(base::ReadFileToString(filename, &contents));
runner->Run(contents, filename.MaybeAsASCII());
}
class BindingsSetupHelper {
public:
explicit BindingsSetupHelper(v8::Isolate* isolate)
: handle_scope_(isolate), delegate_(), runner_(&delegate_, isolate) {
gin::Runner::Scope scope(&runner_);
fidljs::InjectZxBindings(isolate, runner_.global());
// TODO(crbug.com/883496): Figure out how to set up v8 import hooking and
// make fidl_Xyz into $fidl.Xyz. Manually inject the runtime support js
// files for now.
LoadAndSource(&runner_, base::FilePath(kRuntimeFile));
LoadAndSource(&runner_, base::FilePath(kTestBindingFile));
zx_status_t status = zx::channel::create(0, &server_, &client_);
EXPECT_EQ(status, ZX_OK);
runner_.global()->Set(gin::StringToSymbol(isolate, "testHandle"),
gin::ConvertToV8(isolate, client_.get()));
}
zx::channel& server() { return server_; }
zx::channel& client() { return client_; }
gin::ShellRunner& runner() { return runner_; }
private:
v8::HandleScope handle_scope_;
FidlGenJsTestShellRunnerDelegate delegate_;
gin::ShellRunner runner_;
zx::channel server_;
zx::channel client_;
DISALLOW_COPY_AND_ASSIGN(BindingsSetupHelper);
};
TEST_F(FidlGenJsTest, RawReceiveFidlMessage) {
v8::Isolate* isolate = instance_->isolate();
BindingsSetupHelper helper(isolate);
// Send the data from the JS side into the channel.
std::string source = R"(
var proxy = new TestolaProxy();
proxy.$bind(testHandle);
proxy.DoSomething();
)";
helper.runner().Run(source, "test.js");
// Read it out, decode, and confirm it was dispatched.
TestolaImpl testola_impl;
fidljstest::Testola_Stub stub(&testola_impl);
uint8_t data[1024];
zx_handle_t handles[1];
uint32_t actual_bytes, actual_handles;
ASSERT_EQ(helper.server().read(0, data, base::size(data), &actual_bytes,
handles, base::size(handles), &actual_handles),
ZX_OK);
EXPECT_EQ(actual_bytes, 16u);
EXPECT_EQ(actual_handles, 0u);
fidl::Message message(
fidl::BytePart(data, actual_bytes, actual_bytes),
fidl::HandlePart(handles, actual_handles, actual_handles));
stub.Dispatch_(std::move(message), fidl::internal::PendingResponse());
EXPECT_TRUE(testola_impl.was_do_something_called());
}
TEST_F(FidlGenJsTest, RawReceiveFidlMessageWithSimpleArg) {
v8::Isolate* isolate = instance_->isolate();
BindingsSetupHelper helper(isolate);
// Send the data from the JS side into the channel.
std::string source = R"(
var proxy = new TestolaProxy();
proxy.$bind(testHandle);
proxy.PrintInt(12345);
)";
helper.runner().Run(source, "test.js");
// Read it out, decode, and confirm it was dispatched.
TestolaImpl testola_impl;
fidljstest::Testola_Stub stub(&testola_impl);
uint8_t data[1024];
zx_handle_t handles[1];
uint32_t actual_bytes, actual_handles;
ASSERT_EQ(helper.server().read(0, data, base::size(data), &actual_bytes,
handles, base::size(handles), &actual_handles),
ZX_OK);
// 24 rather than 20 because everything's 8 aligned.
EXPECT_EQ(actual_bytes, 24u);
EXPECT_EQ(actual_handles, 0u);
fidl::Message message(
fidl::BytePart(data, actual_bytes, actual_bytes),
fidl::HandlePart(handles, actual_handles, actual_handles));
stub.Dispatch_(std::move(message), fidl::internal::PendingResponse());
EXPECT_EQ(testola_impl.received_int(), 12345);
}
TEST_F(FidlGenJsTest, RawReceiveFidlMessageWithStringArg) {
v8::Isolate* isolate = instance_->isolate();
BindingsSetupHelper helper(isolate);
// Send the data from the JS side into the channel.
std::string source = R"(
var proxy = new TestolaProxy();
proxy.$bind(testHandle);
proxy.PrintMsg('Ça c\'est a 你好 from deep in JS');
)";
helper.runner().Run(source, "test.js");
// Read it out, decode, and confirm it was dispatched.
TestolaImpl testola_impl;
fidljstest::Testola_Stub stub(&testola_impl);
uint8_t data[1024];
zx_handle_t handles[1];
uint32_t actual_bytes, actual_handles;
ASSERT_EQ(helper.server().read(0, data, base::size(data), &actual_bytes,
handles, base::size(handles), &actual_handles),
ZX_OK);
EXPECT_EQ(actual_handles, 0u);
fidl::Message message(
fidl::BytePart(data, actual_bytes, actual_bytes),
fidl::HandlePart(handles, actual_handles, actual_handles));
stub.Dispatch_(std::move(message), fidl::internal::PendingResponse());
EXPECT_EQ(testola_impl.received_msg(), "Ça c'est a 你好 from deep in JS");
}
TEST_F(FidlGenJsTest, RawReceiveFidlMessageWithMultipleArgs) {
v8::Isolate* isolate = instance_->isolate();
BindingsSetupHelper helper(isolate);
// Send the data from the JS side into the channel.
std::string source = R"(
var proxy = new TestolaProxy();
proxy.$bind(testHandle);
proxy.VariousArgs(Blorp.GAMMA, 'zippy zap', [ 999, 987, 123456 ]);
)";
helper.runner().Run(source, "test.js");
// Read it out, decode, and confirm it was dispatched.
TestolaImpl testola_impl;
fidljstest::Testola_Stub stub(&testola_impl);
uint8_t data[1024];
zx_handle_t handles[1];
uint32_t actual_bytes, actual_handles;
ASSERT_EQ(helper.server().read(0, data, base::size(data), &actual_bytes,
handles, base::size(handles), &actual_handles),
ZX_OK);
EXPECT_EQ(actual_handles, 0u);
fidl::Message message(
fidl::BytePart(data, actual_bytes, actual_bytes),
fidl::HandlePart(handles, actual_handles, actual_handles));
stub.Dispatch_(std::move(message), fidl::internal::PendingResponse());
EXPECT_EQ(testola_impl.various_blorp(), fidljstest::Blorp::GAMMA);
EXPECT_EQ(testola_impl.various_msg(), "zippy zap");
ASSERT_EQ(testola_impl.various_stuff().size(), 3u);
EXPECT_EQ(testola_impl.various_stuff()[0], 999u);
EXPECT_EQ(testola_impl.various_stuff()[1], 987u);
EXPECT_EQ(testola_impl.various_stuff()[2], 123456u);
}
int main(int argc, char** argv) {
base::TestSuite test_suite(argc, argv);
return base::LaunchUnitTests(
argc, argv,
base::BindOnce(&base::TestSuite::Run, base::Unretained(&test_suite)));
}
// Copyright 2018 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.
library fidljstest;
enum Blorp : int8 {
ALPHA = 1;
BETA = 2;
GAMMA = 0x48;
};
interface Testola {
1: DoSomething();
2: PrintInt(int32 num);
3: PrintMsg(string msg);
4: VariousArgs(Blorp blorp, string:32 msg, vector<uint32> stuff);
};
Copyright (c) 2013, Ethan Furman.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials
provided with the distribution.
Neither the name Ethan Furman nor the names of any
contributors may be used to endorse or promote products
derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Name: enum34
Short Name: enum34
URL: https://bitbucket.org/stoneleaf/enum34
License: BSD
License File: LICENSE
Revision: f24487b
Security Critical: no
Description:
'Enum' backported from Python 3.4 to earlier Python versions. Only LICENSE and
__init__.py are taken, other packaging files, documentation, etc. removed.
Only used at build time.
"""Python Enumerations"""
import sys as _sys
__all__ = ['Enum', 'IntEnum', 'unique']
version = 1, 1, 6
pyver = float('%s.%s' % _sys.version_info[:2])
try:
any
except NameError:
def any(iterable):
for element in iterable:
if element:
return True
return False
try:
from collections import OrderedDict
except ImportError:
OrderedDict = None
try:
basestring
except NameError:
# In Python 2 basestring is the ancestor of both str and unicode
# in Python 3 it's just str, but was missing in 3.1
basestring = str
try:
unicode
except NameError:
# In Python 3 unicode no longer exists (it's just str)
unicode = str
class _RouteClassAttributeToGetattr(object):
"""Route attribute access on a class to __getattr__.
This is a descriptor, used to define attributes that act differently when
accessed through an instance and through a class. Instance access remains
normal, but access to an attribute through a class will be routed to the
class's __getattr__ method; this is done by raising AttributeError.
"""
def __init__(self, fget=None):
self.fget = fget
def __get__(self, instance, ownerclass=None):
if instance is None:
raise AttributeError()
return self.fget(instance)
def __set__(self, instance, value):
raise AttributeError("can't set attribute")
def __delete__(self, instance):
raise AttributeError("can't delete attribute")
def _is_descriptor(obj):
"""Returns True if obj is a descriptor, False otherwise."""
return (
hasattr(obj, '__get__') or
hasattr(obj, '__set__') or
hasattr(obj, '__delete__'))
def _is_dunder(name):
"""Returns True if a __dunder__ name, False otherwise."""
return (name[:2] == name[-2:] == '__' and
name[2:3] != '_' and
name[-3:-2] != '_' and
len(name) > 4)
def _is_sunder(name):
"""Returns True if a _sunder_ name, False otherwise."""
return (name[0] == name[-1] == '_' and
name[1:2] != '_' and
name[-2:-1] != '_' and
len(name) > 2)
def _make_class_unpicklable(cls):
"""Make the given class un-picklable."""
def _break_on_call_reduce(self, protocol=None):
raise TypeError('%r cannot be pickled' % self)
cls.__reduce_ex__ = _break_on_call_reduce
cls.__module__ = '<unknown>'
class _EnumDict(dict):
"""Track enum member order and ensure member names are not reused.
EnumMeta will use the names found in self._member_names as the
enumeration member names.
"""
def __init__(self):
super(_EnumDict, self).__init__()
self._member_names = []
def __setitem__(self, key, value):
"""Changes anything not dundered or not a descriptor.
If a descriptor is added with the same name as an enum member, the name
is removed from _member_names (this may leave a hole in the numerical
sequence of values).
If an enum member name is used twice, an error is raised; duplicate
values are not checked for.
Single underscore (sunder) names are reserved.
Note: in 3.x __order__ is simply discarded as a not necessary piece
leftover from 2.x
"""
if pyver >= 3.0 and key in ('_order_', '__order__'):
return
elif key == '__order__':
key = '_order_'
if _is_sunder(key):
if key != '_order_':
raise ValueError('_names_ are reserved for future Enum use')
elif _is_dunder(key):
pass
elif key in self._member_names:
# descriptor overwriting an enum?
raise TypeError('Attempted to reuse key: %r' % key)
elif not _is_descriptor(value):
if key in self:
# enum overwriting a descriptor?
raise TypeError('Key already defined as: %r' % self[key])
self._member_names.append(key)
super(_EnumDict, self).__setitem__(key, value)
# Dummy value for Enum as EnumMeta explicity checks for it, but of course until
# EnumMeta finishes running the first time the Enum class doesn't exist. This
# is also why there are checks in EnumMeta like `if Enum is not None`
Enum = None
class EnumMeta(type):
"""Metaclass for Enum"""
@classmethod
def __prepare__(metacls, cls, bases):
return _EnumDict()
def __new__(metacls, cls, bases, classdict):
# an Enum class is final once enumeration items have been defined; it
# cannot be mixed with other types (int, float, etc.) if it has an
# inherited __new__ unless a new __new__ is defined (or the resulting
# class will fail).
if type(classdict) is dict:
original_dict = classdict
classdict = _EnumDict()
for k, v in original_dict.items():
classdict[k] = v
member_type, first_enum = metacls._get_mixins_(bases)
__new__, save_new, use_args = metacls._find_new_(classdict, member_type,
first_enum)
# save enum items into separate mapping so they don't get baked into
# the new class
members = dict((k, classdict[k]) for k in classdict._member_names)
for name in classdict._member_names:
del classdict[name]
# py2 support for definition order
_order_ = classdict.get('_order_')
if _order_ is None:
if pyver < 3.0:
try:
_order_ = [name for (name, value) in sorted(members.items(), key=lambda item: item[1])]
except TypeError:
_order_ = [name for name in sorted(members.keys())]
else:
_order_ = classdict._member_names
else:
del classdict['_order_']
if pyver < 3.0:
_order_ = _order_.replace(',', ' ').split()
aliases = [name for name in members if name not in _order_]
_order_ += aliases
# check for illegal enum names (any others?)
invalid_names = set(members) & set(['mro'])
if invalid_names:
raise ValueError('Invalid enum member name(s): %s' % (
', '.join(invalid_names), ))
# save attributes from super classes so we know if we can take
# the shortcut of storing members in the class dict
base_attributes = set([a for b in bases for a in b.__dict__])
# create our new Enum type
enum_class = super(EnumMeta, metacls).__new__(metacls, cls, bases, classdict)
enum_class._member_names_ = [] # names in random order
if OrderedDict is not None:
enum_class._member_map_ = OrderedDict()
else:
enum_class._member_map_ = {} # name->value map
enum_class._member_type_ = member_type
# Reverse value->name map for hashable values.
enum_class._value2member_map_ = {}
# instantiate them, checking for duplicates as we go
# we instantiate first instead of checking for duplicates first in case
# a custom __new__ is doing something funky with the values -- such as
# auto-numbering ;)
if __new__ is None:
__new__ = enum_class.__new__
for member_name in _order_:
value = members[member_name]
if not isinstance(value, tuple):
args = (value, )
else:
args = value
if member_type is tuple: # special case for tuple enums
args = (args, ) # wrap it one more time
if not use_args or not args:
enum_member = __new__(enum_class)
if not hasattr(enum_member, '_value_'):
enum_member._value_ = value
else:
enum_member = __new__(enum_class, *args)
if not hasattr(enum_member, '_value_'):
enum_member._value_ = member_type(*args)
value = enum_member._value_
enum_member._name_ = member_name
enum_member.__objclass__ = enum_class
enum_member.__init__(*args)
# If another member with the same value was already defined, the
# new member becomes an alias to the existing one.
for name, canonical_member in enum_class._member_map_.items():
if canonical_member.value == enum_member._value_:
enum_member = canonical_member
break
else:
# Aliases don't appear in member names (only in __members__).
enum_class._member_names_.append(member_name)
# performance boost for any member that would not shadow
# a DynamicClassAttribute (aka _RouteClassAttributeToGetattr)
if member_name not in base_attributes:
setattr(enum_class, member_name, enum_member)
# now add to _member_map_
enum_class._member_map_[member_name] = enum_member
try:
# This may fail if value is not hashable. We can't add the value
# to the map, and by-value lookups for this value will be
# linear.
enum_class._value2member_map_[value] = enum_member
except TypeError:
pass
# If a custom type is mixed into the Enum, and it does not know how
# to pickle itself, pickle.dumps will succeed but pickle.loads will
# fail. Rather than have the error show up later and possibly far
# from the source, sabotage the pickle protocol for this class so
# that pickle.dumps also fails.
#
# However, if the new class implements its own __reduce_ex__, do not
# sabotage -- it's on them to make sure it works correctly. We use
# __reduce_ex__ instead of any of the others as it is preferred by
# pickle over __reduce__, and it handles all pickle protocols.
unpicklable = False
if '__reduce_ex__' not in classdict:
if member_type is not object:
methods = ('__getnewargs_ex__', '__getnewargs__',
'__reduce_ex__', '__reduce__')
if not any(m in member_type.__dict__ for m in methods):
_make_class_unpicklable(enum_class)
unpicklable = True
# double check that repr and friends are not the mixin's or various
# things break (such as pickle)
for name in ('__repr__', '__str__', '__format__', '__reduce_ex__'):
class_method = getattr(enum_class, name)
obj_method = getattr(member_type, name, None)
enum_method = getattr(first_enum, name, None)
if name not in classdict and class_method is not enum_method:
if name == '__reduce_ex__' and unpicklable:
continue
setattr(enum_class, name, enum_method)
# method resolution and int's are not playing nice
# Python's less than 2.6 use __cmp__
if pyver < 2.6:
if issubclass(enum_class, int):
setattr(enum_class, '__cmp__', getattr(int, '__cmp__'))
elif pyver < 3.0:
if issubclass(enum_class, int):
for method in (
'__le__',
'__lt__',
'__gt__',
'__ge__',
'__eq__',
'__ne__',
'__hash__',
):
setattr(enum_class, method, getattr(int, method))
# replace any other __new__ with our own (as long as Enum is not None,
# anyway) -- again, this is to support pickle
if Enum is not None:
# if the user defined their own __new__, save it before it gets
# clobbered in case they subclass later
if save_new:
setattr(enum_class, '__member_new__', enum_class.__dict__['__new__'])
setattr(enum_class, '__new__', Enum.__dict__['__new__'])
return enum_class
def __bool__(cls):
"""
classes/types should always be True.
"""
return True
def __call__(cls, value, names=None, module=None, type=None, start=1):
"""Either returns an existing member, or creates a new enum class.
This method is used both when an enum class is given a value to match
to an enumeration member (i.e. Color(3)) and for the functional API
(i.e. Color = Enum('Color', names='red green blue')).
When used for the functional API: `module`, if set, will be stored in
the new class' __module__ attribute; `type`, if set, will be mixed in
as the first base class.
Note: if `module` is not set this routine will attempt to discover the
calling module by walking the frame stack; if this is unsuccessful
the resulting class will not be pickleable.
"""
if names is None: # simple value lookup
return cls.__new__(cls, value)
# otherwise, functional API: we're creating a new Enum type
return cls._create_(value, names, module=module, type=type, start=start)
def __contains__(cls, member):
return isinstance(member, cls) and member.name in cls._member_map_
def __delattr__(cls, attr):
# nicer error message when someone tries to delete an attribute
# (see issue19025).
if attr in cls._member_map_:
raise AttributeError(
"%s: cannot delete Enum member." % cls.__name__)
super(EnumMeta, cls).__delattr__(attr)
def __dir__(self):
return (['__class__', '__doc__', '__members__', '__module__'] +
self._member_names_)
@property
def __members__(cls):
"""Returns a mapping of member name->value.
This mapping lists all enum members, including aliases. Note that this
is a copy of the internal mapping.
"""
return cls._member_map_.copy()
def __getattr__(cls, name):
"""Return the enum member matching `name`
We use __getattr__ instead of descriptors or inserting into the enum
class' __dict__ in order to support `name` and `value` being both
properties for enum members (which live in the class' __dict__) and
enum members themselves.
"""
if _is_dunder(name):
raise AttributeError(name)
try:
return cls._member_map_[name]
except KeyError:
raise AttributeError(name)
def __getitem__(cls, name):
return cls._member_map_[name]
def __iter__(cls):
return (cls._member_map_[name] for name in cls._member_names_)
def __reversed__(cls):
return (cls._member_map_[name] for name in reversed(cls._member_names_))
def __len__(cls):
return len(cls._member_names_)
__nonzero__ = __bool__
def __repr__(cls):
return "<enum %r>" % cls.__name__
def __setattr__(cls, name, value):
"""Block attempts to reassign Enum members.
A simple assignment to the class namespace only changes one of the
several possible ways to get an Enum member from the Enum class,
resulting in an inconsistent Enumeration.
"""
member_map = cls.__dict__.get('_member_map_', {})
if name in member_map:
raise AttributeError('Cannot reassign members.')
super(EnumMeta, cls).__setattr__(name, value)
def _create_(cls, class_name, names=None, module=None, type=None, start=1):
"""Convenience method to create a new Enum class.
`names` can be:
* A string containing member names, separated either with spaces or
commas. Values are auto-numbered from 1.
* An iterable of member names. Values are auto-numbered from 1.
* An iterable of (member name, value) pairs.
* A mapping of member name -> value.
"""
if pyver < 3.0:
# if class_name is unicode, attempt a conversion to ASCII
if isinstance(class_name, unicode):
try:
class_name = class_name.encode('ascii')
except UnicodeEncodeError:
raise TypeError('%r is not representable in ASCII' % class_name)
metacls = cls.__class__
if type is None:
bases = (cls, )
else:
bases = (type, cls)
classdict = metacls.__prepare__(class_name, bases)
_order_ = []
# special processing needed for names?
if isinstance(names, basestring):
names = names.replace(',', ' ').split()
if isinstance(names, (tuple, list)) and isinstance(names[0], basestring):
names = [(e, i+start) for (i, e) in enumerate(names)]
# Here, names is either an iterable of (name, value) or a mapping.
item = None # in case names is empty
for item in names:
if isinstance(item, basestring):
member_name, member_value = item, names[item]
else:
member_name, member_value = item
classdict[member_name] = member_value
_order_.append(member_name)
# only set _order_ in classdict if name/value was not from a mapping
if not isinstance(item, basestring):
classdict['_order_'] = ' '.join(_order_)
enum_class = metacls.__new__(metacls, class_name, bases, classdict)
# TODO: replace the frame hack if a blessed way to know the calling
# module is ever developed
if module is None:
try:
module = _sys._getframe(2).f_globals['__name__']
except (AttributeError, ValueError):
pass
if module is None:
_make_class_unpicklable(enum_class)
else:
enum_class.__module__ = module
return enum_class
@staticmethod
def _get_mixins_(bases):
"""Returns the type for creating enum members, and the first inherited
enum class.
bases: the tuple of bases that was given to __new__
"""
if not bases or Enum is None:
return object, Enum
# double check that we are not subclassing a class with existing
# enumeration members; while we're at it, see if any other data
# type has been mixed in so we can use the correct __new__
member_type = first_enum = None
for base in bases:
if (base is not Enum and
issubclass(base, Enum) and
base._member_names_):
raise TypeError("Cannot extend enumerations")
# base is now the last base in bases
if not issubclass(base, Enum):
raise TypeError("new enumerations must be created as "
"`ClassName([mixin_type,] enum_type)`")
# get correct mix-in type (either mix-in type of Enum subclass, or
# first base if last base is Enum)
if not issubclass(bases[0], Enum):
member_type = bases[0] # first data type
first_enum = bases[-1] # enum type
else:
for base in bases[0].__mro__:
# most common: (IntEnum, int, Enum, object)
# possible: (<Enum 'AutoIntEnum'>, <Enum 'IntEnum'>,
# <class 'int'>, <Enum 'Enum'>,
# <class 'object'>)
if issubclass(base, Enum):
if first_enum is None:
first_enum = base
else:
if member_type is None:
member_type = base
return member_type, first_enum
if pyver < 3.0:
@staticmethod
def _find_new_(classdict, member_type, first_enum):
"""Returns the __new__ to be used for creating the enum members.
classdict: the class dictionary given to __new__
member_type: the data type whose __new__ will be used by default
first_enum: enumeration to check for an overriding __new__
"""
# now find the correct __new__, checking to see of one was defined
# by the user; also check earlier enum classes in case a __new__ was
# saved as __member_new__
__new__ = classdict.get('__new__', None)
if __new__:
return None, True, True # __new__, save_new, use_args
N__new__ = getattr(None, '__new__')
O__new__ = getattr(object, '__new__')
if Enum is None:
E__new__ = N__new__
else:
E__new__ = Enum.__dict__['__new__']
# check all possibles for __member_new__ before falling back to
# __new__
for method in ('__member_new__', '__new__'):
for possible in (member_type, first_enum):
try:
target = possible.__dict__[method]
except (AttributeError, KeyError):
target = getattr(possible, method, None)
if target not in [
None,
N__new__,
O__new__,
E__new__,
]:
if method == '__member_new__':
classdict['__new__'] = target
return None, False, True
if isinstance(target, staticmethod):
target = target.__get__(member_type)
__new__ = target
break
if __new__ is not None:
break
else:
__new__ = object.__new__
# if a non-object.__new__ is used then whatever value/tuple was
# assigned to the enum member name will be passed to __new__ and to the
# new enum member's __init__
if __new__ is object.__new__:
use_args = False
else:
use_args = True
return __new__, False, use_args
else:
@staticmethod
def _find_new_(classdict, member_type, first_enum):
"""Returns the __new__ to be used for creating the enum members.
classdict: the class dictionary given to __new__
member_type: the data type whose __new__ will be used by default
first_enum: enumeration to check for an overriding __new__
"""
# now find the correct __new__, checking to see of one was defined
# by the user; also check earlier enum classes in case a __new__ was
# saved as __member_new__
__new__ = classdict.get('__new__', None)
# should __new__ be saved as __member_new__ later?
save_new = __new__ is not None
if __new__ is None:
# check all possibles for __member_new__ before falling back to
# __new__
for method in ('__member_new__', '__new__'):
for possible in (member_type, first_enum):
target = getattr(possible, method, None)
if target not in (
None,
None.__new__,
object.__new__,
Enum.__new__,
):
__new__ = target
break
if __new__ is not None:
break
else:
__new__ = object.__new__
# if a non-object.__new__ is used then whatever value/tuple was
# assigned to the enum member name will be passed to __new__ and to the
# new enum member's __init__
if __new__ is object.__new__:
use_args = False
else:
use_args = True
return __new__, save_new, use_args
########################################################
# In order to support Python 2 and 3 with a single
# codebase we have to create the Enum methods separately
# and then use the `type(name, bases, dict)` method to
# create the class.
########################################################
temp_enum_dict = {}
temp_enum_dict['__doc__'] = "Generic enumeration.\n\n Derive from this class to define new enumerations.\n\n"
def __new__(cls, value):
# all enum instances are actually created during class construction
# without calling this method; this method is called by the metaclass'
# __call__ (i.e. Color(3) ), and by pickle
if type(value) is cls:
# For lookups like Color(Color.red)
value = value.value
#return value
# by-value search for a matching enum member
# see if it's in the reverse mapping (for hashable values)
try:
if value in cls._value2member_map_:
return cls._value2member_map_[value]
except TypeError:
# not there, now do long search -- O(n) behavior
for member in cls._member_map_.values():
if member.value == value:
return member
raise ValueError("%s is not a valid %s" % (value, cls.__name__))
temp_enum_dict['__new__'] = __new__
del __new__
def __repr__(self):
return "<%s.%s: %r>" % (
self.__class__.__name__, self._name_, self._value_)
temp_enum_dict['__repr__'] = __repr__
del __repr__
def __str__(self):
return "%s.%s" % (self.__class__.__name__, self._name_)
temp_enum_dict['__str__'] = __str__
del __str__
if pyver >= 3.0:
def __dir__(self):
added_behavior = [
m
for cls in self.__class__.mro()
for m in cls.__dict__
if m[0] != '_' and m not in self._member_map_
]
return (['__class__', '__doc__', '__module__', ] + added_behavior)
temp_enum_dict['__dir__'] = __dir__
del __dir__
def __format__(self, format_spec):
# mixed-in Enums should use the mixed-in type's __format__, otherwise
# we can get strange results with the Enum name showing up instead of
# the value
# pure Enum branch
if self._member_type_ is object:
cls = str
val = str(self)
# mix-in branch
else:
cls = self._member_type_
val = self.value
return cls.__format__(val, format_spec)
temp_enum_dict['__format__'] = __format__
del __format__
####################################
# Python's less than 2.6 use __cmp__
if pyver < 2.6:
def __cmp__(self, other):
if type(other) is self.__class__:
if self is other:
return 0
return -1
return NotImplemented
raise TypeError("unorderable types: %s() and %s()" % (self.__class__.__name__, other.__class__.__name__))
temp_enum_dict['__cmp__'] = __cmp__
del __cmp__
else:
def __le__(self, other):
raise TypeError("unorderable types: %s() <= %s()" % (self.__class__.__name__, other.__class__.__name__))
temp_enum_dict['__le__'] = __le__
del __le__
def __lt__(self, other):
raise TypeError("unorderable types: %s() < %s()" % (self.__class__.__name__, other.__class__.__name__))
temp_enum_dict['__lt__'] = __lt__
del __lt__
def __ge__(self, other):
raise TypeError("unorderable types: %s() >= %s()" % (self.__class__.__name__, other.__class__.__name__))
temp_enum_dict['__ge__'] = __ge__
del __ge__
def __gt__(self, other):
raise TypeError("unorderable types: %s() > %s()" % (self.__class__.__name__, other.__class__.__name__))
temp_enum_dict['__gt__'] = __gt__
del __gt__
def __eq__(self, other):
if type(other) is self.__class__:
return self is other
return NotImplemented
temp_enum_dict['__eq__'] = __eq__
del __eq__
def __ne__(self, other):
if type(other) is self.__class__:
return self is not other
return NotImplemented
temp_enum_dict['__ne__'] = __ne__
del __ne__
def __hash__(self):
return hash(self._name_)
temp_enum_dict['__hash__'] = __hash__
del __hash__
def __reduce_ex__(self, proto):
return self.__class__, (self._value_, )
temp_enum_dict['__reduce_ex__'] = __reduce_ex__
del __reduce_ex__
# _RouteClassAttributeToGetattr is used to provide access to the `name`
# and `value` properties of enum members while keeping some measure of
# protection from modification, while still allowing for an enumeration
# to have members named `name` and `value`. This works because enumeration
# members are not set directly on the enum class -- __getattr__ is
# used to look them up.
@_RouteClassAttributeToGetattr
def name(self):
return self._name_
temp_enum_dict['name'] = name
del name
@_RouteClassAttributeToGetattr
def value(self):
return self._value_
temp_enum_dict['value'] = value
del value
@classmethod
def _convert(cls, name, module, filter, source=None):
"""
Create a new Enum subclass that replaces a collection of global constants
"""
# convert all constants from source (or module) that pass filter() to
# a new Enum called name, and export the enum and its members back to
# module;
# also, replace the __reduce_ex__ method so unpickling works in
# previous Python versions
module_globals = vars(_sys.modules[module])
if source:
source = vars(source)
else:
source = module_globals
members = dict((name, value) for name, value in source.items() if filter(name))
cls = cls(name, members, module=module)
cls.__reduce_ex__ = _reduce_ex_by_name
module_globals.update(cls.__members__)
module_globals[name] = cls
return cls
temp_enum_dict['_convert'] = _convert
del _convert
Enum = EnumMeta('Enum', (object, ), temp_enum_dict)
del temp_enum_dict
# Enum has now been created
###########################
class IntEnum(int, Enum):
"""Enum where members are also (and must be) ints"""
def _reduce_ex_by_name(self, proto):
return self.name
def unique(enumeration):
"""Class decorator that ensures only unique members exist in an enumeration."""
duplicates = []
for name, member in enumeration.__members__.items():
if name != member.name:
duplicates.append((name, member.name))
if duplicates:
duplicate_names = ', '.join(
["%s -> %s" % (alias, name) for (alias, name) in duplicates]
)
raise ValueError('duplicate names found in %r: %s' %
(enumeration, duplicate_names)
)
return enumeration
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