@@ -29,6 +29,13 @@ import { MODULE_DEF } from './schema';
2929
3030import * as _syscalls from 'spacetime:sys@1.0' ;
3131import type { u16 , u32 , ModuleHooks } from 'spacetime:sys@1.0' ;
32+ import {
33+ ANON_VIEWS ,
34+ VIEWS ,
35+ type AnonymousViewCtx ,
36+ type ViewCtx ,
37+ } from './views' ;
38+ import { bsatnBaseSize } from './util' ;
3239
3340const { freeze } = Object ;
3441
@@ -212,6 +219,46 @@ export const hooks: ModuleHooks = {
212219 } ,
213220} ;
214221
222+ export const hooks_v1_1 : import ( 'spacetime:sys@1.1' ) . ModuleHooks = {
223+ __call_view__ ( id , sender , argsBuf ) {
224+ const { fn, params, returnType, returnTypeBaseSize } = VIEWS [ id ] ;
225+ const ctx : ViewCtx < any > = freeze ( {
226+ sender : new Identity ( sender ) ,
227+ // this is the non-readonly DbView, but the typing for the user will be
228+ // the readonly one, and if they do call mutating functions it will fail
229+ // at runtime
230+ db : getDbView ( ) ,
231+ } ) ;
232+ const args = AlgebraicType . deserializeValue (
233+ new BinaryReader ( argsBuf ) ,
234+ AlgebraicType . Product ( params ) ,
235+ MODULE_DEF . typespace
236+ ) ;
237+ const ret = fn ( ctx , args ) ;
238+ const retBuf = new BinaryWriter ( returnTypeBaseSize ) ;
239+ AlgebraicType . serializeValue ( retBuf , returnType , ret , MODULE_DEF . typespace ) ;
240+ return retBuf . getBuffer ( ) ;
241+ } ,
242+ __call_view_anon__ ( id , argsBuf ) {
243+ const { fn, params, returnType, returnTypeBaseSize } = ANON_VIEWS [ id ] ;
244+ const ctx : AnonymousViewCtx < any > = freeze ( {
245+ // this is the non-readonly DbView, but the typing for the user will be
246+ // the readonly one, and if they do call mutating functions it will fail
247+ // at runtime
248+ db : getDbView ( ) ,
249+ } ) ;
250+ const args = AlgebraicType . deserializeValue (
251+ new BinaryReader ( argsBuf ) ,
252+ AlgebraicType . Product ( params ) ,
253+ MODULE_DEF . typespace
254+ ) ;
255+ const ret = fn ( ctx , args ) ;
256+ const retBuf = new BinaryWriter ( returnTypeBaseSize ) ;
257+ AlgebraicType . serializeValue ( retBuf , returnType , ret , MODULE_DEF . typespace ) ;
258+ return retBuf . getBuffer ( ) ;
259+ } ,
260+ } ;
261+
215262let DB_VIEW : DbView < any > | null = null ;
216263function getDbView ( ) {
217264 DB_VIEW ??= makeDbView ( MODULE_DEF ) ;
@@ -464,47 +511,6 @@ function makeTableView(typespace: Typespace, table: RawTableDefV9): Table<any> {
464511 return freeze ( tableView ) ;
465512}
466513
467- function bsatnBaseSize ( typespace : Typespace , ty : AlgebraicType ) : number {
468- const assumedArrayLength = 4 ;
469- while ( ty . tag === 'Ref' ) ty = typespace . types [ ty . value ] ;
470- if ( ty . tag === 'Product' ) {
471- let sum = 0 ;
472- for ( const { algebraicType : elem } of ty . value . elements ) {
473- sum += bsatnBaseSize ( typespace , elem ) ;
474- }
475- return sum ;
476- } else if ( ty . tag === 'Sum' ) {
477- let min = Infinity ;
478- for ( const { algebraicType : vari } of ty . value . variants ) {
479- const vSize = bsatnBaseSize ( typespace , vari ) ;
480- if ( vSize < min ) min = vSize ;
481- }
482- if ( min === Infinity ) min = 0 ;
483- return 4 + min ;
484- } else if ( ty . tag == 'Array' ) {
485- return 4 + assumedArrayLength * bsatnBaseSize ( typespace , ty . value ) ;
486- }
487- return {
488- String : 4 + assumedArrayLength ,
489- Sum : 1 ,
490- Bool : 1 ,
491- I8 : 1 ,
492- U8 : 1 ,
493- I16 : 2 ,
494- U16 : 2 ,
495- I32 : 4 ,
496- U32 : 4 ,
497- F32 : 4 ,
498- I64 : 8 ,
499- U64 : 8 ,
500- F64 : 8 ,
501- I128 : 16 ,
502- U128 : 16 ,
503- I256 : 32 ,
504- U256 : 32 ,
505- } [ ty . tag ] ;
506- }
507-
508514function hasOwn < K extends PropertyKey > (
509515 o : object ,
510516 k : K
0 commit comments