author jdiaz5513
  import * as s from "capnp-ts/src/std/schema.capnp.js" ; import * as capnp from "capnp-ts" ; import { format , pad } from "capnp-ts/src/util" ; import ts , { factory as f } from "typescript" ; import initTrace from "debug" ; import { CodeGeneratorFileContext } from "./code-generator-file-context" ; import { __ , READONLY , STATIC , VOID_TYPE , CAPNP } from "./constants" ; import * as E from "./errors" ; import { getDisplayNamePrefix , getFullClassName , getJsType } from "./file" ; import * as util from "./util" ; const trace = initTrace ( "capnpc:ast-creators" ) ; export function createClassExtends ( identifierText ) { const types = [ f . createExpressionWithTypeArguments ( f . createIdentifier ( identifierText ) , [ ] ) ] ; return f . createHeritageClause ( ts . SyntaxKind . ExtendsKeyword , types ) ; } export function createConcreteListProperty ( ctx , field ) { const name = `_${ util . c2t ( field . getName ( ) ) }` ; const type = f . createTypeReferenceNode ( getJsType ( ctx , field . getSlot ( ) . getType ( ) , true ) , __ ) ; let u ; return f . createPropertyDeclaration ( __ , [ STATIC ] , name , __ , type , u as ts . Expression ) ; } export function createConstProperty ( node ) { const name = util . c2s ( getDisplayNamePrefix ( node ) ) ; const initializer = createValueExpression ( node . getConst ( ) . getValue ( ) ) ; return f . createPropertyDeclaration ( __ , [ STATIC , READONLY ] , name , __ , __ , initializer ) ; } export function createExpressionBlock ( expressions , returns , allowSingleLine ) { const statements = expressions . map ( ( e , i ) => i === expressions . length - 1 && returns ? f . createReturnStatement ( e ) : f . createExpressionStatement ( e ) ) ; return f . createBlock ( statements , ! ( allowSingleLine && expressions . length < 2 ) ) ; } export function createMethod ( name , parameters , type , expressions , allowSingleLine = true ) { return f . createMethodDeclaration ( __ , __ , __ , name , __ , __ , parameters , type , createExpressionBlock ( expressions , type !== VOID_TYPE , allowSingleLine ) ) ; } export function createNestedNodeProperty ( node ) { const name = getDisplayNamePrefix ( node ) ; const initializer = f . createIdentifier ( getFullClassName ( node ) ) ; return f . createPropertyDeclaration ( __ , [ STATIC , READONLY ] , name , __ , __ , initializer ) ; } export function createUnionConstProperty ( fullClassName , field ) { const name = util . c2s ( field . getName ( ) ) ; const initializer = f . createPropertyAccessExpression ( f . createIdentifier ( `${ fullClassName }_Which` ) , name ) ; return f . createPropertyDeclaration ( __ , [ STATIC , READONLY ] , name , __ , __ , initializer ) ; } export function createValueExpression ( value ) { trace ( "createValueExpression(%s)" , value ) ; let p ; switch ( value . which ( ) ) { case s . Value . BOOL : return value . getBool ( ) ? f . createTrue ( ) : f . createFalse ( ) ; case s . Value . ENUM : return f . createNumericLiteral ( value . getEnum ( ) . toString ( ) ) ; case s . Value . FLOAT32 : return f . createNumericLiteral ( value . getFloat32 ( ) . toString ( ) ) ; case s . Value . FLOAT64 : return f . createNumericLiteral ( value . getFloat64 ( ) . toString ( ) ) ; case s . Value . INT16 : return f . createNumericLiteral ( value . getInt16 ( ) . toString ( ) ) ; case s . Value . INT32 : return f . createNumericLiteral ( value . getInt32 ( ) . toString ( ) ) ; case s . Value . INT64 : { let v = value . getInt64 ( ) . toString ( 16 ) ; let neg = "" ; if ( v [ 0 ] === "-" ) { v = v . slice ( 1 ) ; neg = "-" ; } return f . createCallExpression ( f . createIdentifier ( `${ neg }BigInt` ) , __ , [ f . createStringLiteral ( `0x${ v }` ) ] ) ; } case s . Value . INT8 : return f . createNumericLiteral ( value . getInt8 ( ) . toString ( ) ) ; case s . Value . TEXT : return f . createStringLiteral ( value . getText ( ) ) ; case s . Value . UINT16 : return f . createNumericLiteral ( value . getUint16 ( ) . toString ( ) ) ; case s . Value . UINT32 : return f . createNumericLiteral ( value . getUint32 ( ) . toString ( ) ) ; case s . Value . UINT64 : { return f . createCallExpression ( f . createIdentifier ( "BigInt" ) , __ , [ f . createStringLiteral ( `0x${ value . getUint64 ( ) . toString ( 16 ) }` ) , ] ) ; } case s . Value . UINT8 : return f . createNumericLiteral ( value . getUint8 ( ) . toString ( ) ) ; case s . Value . VOID : return f . createIdentifier ( "undefined" ) ; case s . Value . ANY_POINTER : p = value . getAnyPointer ( ) ; break ; case s . Value . DATA : p = value . getData ( ) ; break ; case s . Value . LIST : p = value . getList ( ) ; break ; case s . Value . STRUCT : p = value . getStruct ( ) ; break ; case s . Value . INTERFACE : default : throw new Error ( format ( E . GEN_SERIALIZE_UNKNOWN_VALUE , s . Value_Which [ value . which ( ) ] ) ) ; } const m = new capnp . Message ( ) ; m . setRoot ( p ) ; const buf = new Uint8Array ( m . toPackedArrayBuffer ( ) ) ; const bytes = new Array < ts . NumericLiteral > ( buf . byteLength ) ; for ( let i = 0 ; i < buf . byteLength ; i ++ ) { bytes [ i ] = f . createNumericLiteral ( `0x${ pad ( buf [ i ] . toString ( 16 ) , 2 ) }` ) ; } return f . createCallExpression ( f . createPropertyAccessExpression ( CAPNP , "readRawPointer" ) , __ , [ f . createPropertyAccessExpression ( f . createNewExpression ( f . createIdentifier ( "Uint8Array" ) , __ , [ f . createArrayLiteralExpression ( bytes , false ) ] ) , "buffer" ) , ] ) ; }