Skip to content

Commit

Permalink
Introduce Fragments class which is to be upper concept of interface. (#…
Browse files Browse the repository at this point in the history
…84)

Current implementation is focused to interfaces, but Fragments are needed
to implement other things.
(eg. enums, dictionaries, typedefs and implements statements)

https://heycam.github.io/webidl/#dfn-idl-fragment

ISSUE=#80
  • Loading branch information
hwanseung authored and romandev committed Oct 18, 2017
1 parent 8ed8875 commit 1d2bd7b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
49 changes: 45 additions & 4 deletions generator/idl_parser/idls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,22 @@ export interface InterfaceMember extends TypedIdentifier {
arguments: Array<Argument>;
}

export interface Interface {
const enum DefinitionType {
Interface,
Enum,
}

export interface Definition {
name: string;
type: DefinitionType;
}

export interface Interface extends Definition {
members: Array<InterfaceMember>;
}

export interface Enum extends Definition {}

export class ArgumentImpl implements Argument {
name: string;
type: string;
Expand Down Expand Up @@ -64,13 +75,43 @@ export class InterfaceMemberImpl implements InterfaceMember {
export class InterfaceImpl implements Interface {
name: string;
members: Array<InterfaceMemberImpl>;
type: DefinitionType;

constructor(raw_idl_info: any) {
this.name = raw_idl_info.name;
constructor(interface_info: any) {
this.name = interface_info.name;
this.type = DefinitionType.Interface;

this.members = new Array<InterfaceMemberImpl>();
raw_idl_info.members.forEach(member => {
interface_info.members.forEach(member => {
this.members.push(new InterfaceMemberImpl(member));
});
}
}

export class EnumImpl implements Enum {
name: string;
type: DefinitionType;

// FIXME(hwanseung): should be implement Enum
constructor(enum_info: any) {
this.name = enum_info.name;
this.type = DefinitionType.Enum;
}
}

export class Fragments {
definitions: Array<Definition>;

constructor(raw_idl_infos: any) {
raw_idl_infos.forEach(raw_idl_info => {
this.definitions = new Array<Definition>();
if (raw_idl_info['type'] == 'interface') {
this.definitions.push(new InterfaceImpl(raw_idl_info));
} else if (raw_idl_info['type'] == 'enum') {
this.definitions.push(new EnumImpl(raw_idl_info));
} else {
// FIXME: should implement dictionaries, typedefs or etc.
}
});
}
}
11 changes: 8 additions & 3 deletions generator/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async function generateInterface(
env: nunjucks.Environment, input_idl_path: string, output_path: string) {
const parsedData =
webidl.parse(await file.read(path.resolve(input_idl_path)));
const idl_interface: idls.Interface = new idls.InterfaceImpl(parsedData[0]);
const idl_fragments: idls.Fragments = new idls.Fragments(parsedData);

const [header_tmpl, cpp_tmpl] = await Promise.all([
file.read(path.resolve(TEMPLATE_DIR, 'interface_header.njk')),
Expand All @@ -37,9 +37,14 @@ async function generateInterface(
const header_file_path = path.resolve(output_path, idl_name + '_bridge.h');
const cpp_file_path = path.resolve(output_path, idl_name + '_bridge.cc');

// FIXME: each files can have one more interfaces. and this definition should
// be distinguished it is interface or not.
return Promise.all([
file.write(header_file_path, env.renderString(header_tmpl, idl_interface)),
file.write(cpp_file_path, env.renderString(cpp_tmpl, idl_interface))
file.write(
header_file_path,
env.renderString(header_tmpl, idl_fragments.definitions[0])),
file.write(
cpp_file_path, env.renderString(cpp_tmpl, idl_fragments.definitions[0]))
]);
}

Expand Down

0 comments on commit 1d2bd7b

Please sign in to comment.