public void HandleComponentType(TypeDefinition type)
        {
            var tmpl =
                @"
{{{doc}}}class {{{class}}} extends ut.Component {
  {{{constructor}}}
  {{{fields}}}
  {{{staticfields}}}
  {{{methods}}}

  static readonly cid: number;
  static readonly _view: any;
  static readonly _isSharedComp: boolean;

  static _size: number;
  static _fromPtr(p: number, v?: {{{class}}}): {{{class}}};
  static _toPtr(p: number, v: {{{class}}}): void;
  static _tempHeapPtr(v: {{{class}}}): number;
  static _dtorFn(v: {{{class}}}): void;
}
";

            var constructor  = ConstructorForValueType(type);
            var fields       = type.Fields.Select(StructFieldDeclFor).ToArray();
            var staticfields = type.Fields.Select(StaticStructFieldDeclFor).ToArray();
            var methods      = type.MemberFunctions("JSHide").Select(MethodDeclarationFor).ToArray();

            tsNs(type, CppBindingsGenerator.ExpandStringTemplate(tmpl,
                                                                 "doc", type.JSDocComment(),
                                                                 "constructor", constructor,
                                                                 "class", type.TSName(),
                                                                 "fields", fields,
                                                                 "methods", methods,
                                                                 "staticfields", staticfields));
        }
        public void HandleStructType(TypeDefinition type)
        {
            if (type.HasAttribute("JSCustomImpl"))
            {
                string aliasName = null;
                switch (type.FullName)
                {
                case "UTiny.Math.Vector2": aliasName = "utmath.Vector2"; break;

                case "UTiny.Math.Vector3": aliasName = "utmath.Vector3"; break;

                case "UTiny.Math.Vector4": aliasName = "utmath.Vector4"; break;

                case "UTiny.Math.Quaternion": aliasName = "utmath.Quaternion"; break;

                case "UTiny.Math.Matrix3x3": aliasName = "utmath.Matrix3"; break;

                case "UTiny.Math.Matrix4x4": aliasName = "utmath.Matrix4"; break;

                default:
                    throw new InvalidOperationException($"Don't know how to generate TypeScript custom def for {type.FullName}");
                }
                tsNs(type, $"export import {type.TSName()} = {aliasName};");
                return;
            }

            var tmpl =
                @"
{{{doc}}}class {{{class}}} {{{extends}}}{
  {{{constructor}}}
  {{{fields}}}
  {{{methods}}}

  static _size: number;
  static _fromPtr(p: number, v?: {{{class}}}): {{{class}}};
  static _toPtr(p: number, v: {{{class}}}): void;
  static _tempHeapPtr(v: {{{class}}}): number;
}
interface {{{class}}}ComponentFieldDesc extends ut.ComponentFieldDesc {
  {{{staticfields}}}
}
";

            var constructor  = ConstructorForValueType(type);
            var extends      = type.GetValueOfAttribute("TSExtends");
            var fields       = type.Fields.Select(StructFieldDeclFor).ToArray();
            var staticfields = type.Fields.Select(StaticStructFieldDeclFor).ToArray();
            var methods      = type.MemberFunctions("JSHide").Select(MethodDeclarationFor).ToArray();

            tsNs(type, CppBindingsGenerator.ExpandStringTemplate(tmpl,
                                                                 "doc", type.JSDocComment(),
                                                                 "constructor", constructor,
                                                                 "class", type.TSName(),
                                                                 "extends", extends != null ? $"extends {extends} " : "",
                                                                 "fields", fields,
                                                                 "methods", methods,
                                                                 "staticfields", staticfields));
        }
        public void HandleCallbackType(TypeDefinition type)
        {
            var tmpl =
                @"{{{doc}}}type {{{cb}}} = {{{cbproto}}};";

            var name    = type.TSName();
            var cbproto = MethodTypeDeclarationFor(type.DelegateInvokeMethod());

            tsNs(type, CppBindingsGenerator.ExpandStringTemplate(tmpl,
                                                                 "doc", type.JSDocComment(),
                                                                 "cb", name,
                                                                 "cbproto", cbproto));
        }
        public void HandleInterfaceType(TypeDefinition type)
        {
            var tmpl =
                @"{{{doc}}}class {{{class}}} {
  {{{methods}}}
}";
            var methods = type.MemberFunctions("JSHide").Select(MethodDeclarationFor).ToArray();

            tsNs(type, CppBindingsGenerator.ExpandStringTemplate(tmpl,
                                                                 "doc", type.JSDocComment(),
                                                                 "class", type.TSName(),
                                                                 "methods", methods));
        }
        public void HandleEnumType(TypeDefinition type)
        {
            var tmpl =
                @"{{{doc}}}enum {{{enum}}} {
  {{{values}}}
}";

            var values = type.GetEnumKeyValues().Select(m => $"{m.documentation}{m.key} = {m.value},").ToArray();

            tsNs(type, CppBindingsGenerator.ExpandStringTemplate(tmpl,
                                                                 "doc", type.JSDocComment(),
                                                                 "enum", type.TSName(),
                                                                 "values", values));
        }
Ejemplo n.º 6
0
        public void HandleInterfaceType(TypeDefinition type)
        {
            if (type.HasAttribute("PureJSService"))
            {
                return;
            }

            BindGem.FatalErrorIf(BindGem.PureJS,
                                 "Interfaces cannot be used with pure JS generation (what are you trying to bind anyway?)");

            var jsTypeName    = type.JSName();
            var cppTypeName   = type.FullyQualifiedCppName();
            var cppFnTypeName = type.CppFnPrefix();

            // C# interface with [SharedPtr]
            bool isSharedPtr    = type.IsSharedPtrType();
            bool isNonSharedPtr = type.IsNonSharedPtrType();
            // C# interface with [Service]
            bool isService = type.IsServiceRefType();
            // C# struct
            bool isStruct = type.IsStructValueType();

            //
            // Constructors and preamble
            //
            // At some point in the future we may support multiple constructors and constructors with arguments,
            // and some way to specify them.
            // For now, we have a Constructable attribute that enables a single, no-argument constructor.
            bool isConstructable = type.HasAttribute("Constructable");

            if (isConstructable)
            {
                dts($"function _{type.CppFnPrefix()}_{type.Name}(): number;");

                exportSymbol($"{type.CppFnPrefix()}_{type.Name}");
                cpp($"UT_JSFN {cppTypeName}* {type.CppFnPrefix()}_{type.Name}() {{");
                if (isSharedPtr)
                {
                    cpp($"  return ut::PtrTable::persist<{cppTypeName}>(std::make_shared<{cppTypeName}>());");
                }
                else
                {
                    cpp($"  return new {cppTypeName}();");
                }
                cpp($"}}");
            }

            if (isSharedPtr || isNonSharedPtr)
            {
                string releaseTemplate;
                exportSymbol($"{cppFnTypeName}_shRelease");
                if (isSharedPtr)
                {
                    releaseTemplate =
                        @"
UT_JSFN void {{{cppFnTypeName}}}_shRelease({{{cppTypeName}}}* ptr) {
  ut::PtrTable::release<{{{cppTypeName}}}>(ptr);
}
";
                }
                else
                {
                    releaseTemplate =
                        @"UT_JSFN void {{{cppFnTypeName}}}_shRelease({{{cppTypeName}}}* ptr) {
  delete ptr;
}";
                }

                dts($"function _{type.CppFnPrefix()}_shRelease(self: number): void;");

                var releaseFnString = CppBindingsGenerator.ExpandStringTemplate(releaseTemplate,
                                                                                "cppTypeName", cppTypeName,
                                                                                "cppFnTypeName", cppFnTypeName);
                cpp(releaseFnString);
            }

            HandleTypeMethods(type);
        }