@@ -266,6 +266,7 @@ function node_loader_trampoline_discover_function(func) {
266266 if ( node_loader_trampoline_is_callable ( func ) ) {
267267 // Espree can't parse native code functions so we can do a workaround
268268 const str = func . toString ( ) . replace ( '{ [native code] }' , '{}' ) ;
269+ //console.log(str)
269270 const ast = espree . parse ( `(${ str } )` , {
270271 ecmaVersion : 14
271272 } ) ;
@@ -276,7 +277,7 @@ function node_loader_trampoline_discover_function(func) {
276277 if ( node_loader_trampoline_is_valid_symbol ( node ) ) {
277278 const args = node_loader_trampoline_discover_arguments ( node ) ;
278279 const discover = {
279- ptr : func ,
280+ func,
280281 signature : args ,
281282 async : node . async ,
282283 } ;
@@ -293,6 +294,96 @@ function node_loader_trampoline_discover_function(func) {
293294 }
294295}
295296
297+ function node_loader_trampoline_discover_klass_attributes ( node ) {
298+ let attributes = [ ]
299+ for ( let i = 0 ; i < node . length ; i ++ ) {
300+ if ( node [ i ] . kind === 'constructor' )
301+ {
302+ for ( let exp of node [ i ] . value . body . body )
303+ {
304+ if ( exp . type === 'ExpressionStatement' && exp . expression . type === 'AssignmentExpression' ) {
305+ let left = exp . expression . left
306+
307+ if ( left . type == 'MemberExpression' && ( left . object && left . object . type === 'ThisExpression' ) ) {
308+ attributes . push ( left . property && left . property . name )
309+ }
310+ }
311+ }
312+ }
313+ }
314+
315+ return attributes
316+ }
317+
318+ function node_loader_trampoline_discover_klass_methods ( node , str ) {
319+ const ret = { }
320+ for ( let method of node ) {
321+ if ( method . type === 'MethodDefinition' ) {
322+ let method_name = method . key . name
323+ if ( method . kind === 'constructor' ) {
324+ method_name = 'klass_' + method_name
325+ }
326+ ret [ method_name ] = {
327+ name : method . key . name ,
328+ signature : node_loader_trampoline_discover_arguments ( method . value )
329+ }
330+
331+ if ( method . kind === 'method' && str . substring ( method . start - 1 , method . start + 5 ) === 'static' ) {
332+ ret [ method_name ] . static = true
333+ }
334+ }
335+ }
336+
337+ return ret
338+ }
339+
340+ function node_loader_trampoline_discover_klass ( klass ) {
341+ try {
342+ if ( node_loader_trampoline_is_callable ( klass ) ) {
343+ const str = klass . toString ( ) ;
344+ const ast = espree . parse ( `(${ str } )` , {
345+ ecmaVersion : 14
346+ } ) ;
347+
348+ //console.log(ast)
349+
350+ const node = ( ast . body [ 0 ] . type === 'ExpressionStatement' ) && ast . body [ 0 ] . expression ;
351+ console . log ( node . body . body [ 2 ] )
352+ if ( node . type === 'ClassExpression' ) {
353+ const methods = node_loader_trampoline_discover_klass_methods ( node . body . body , str )
354+ console . log ( str . substring ( 119 - 1 , 119 + 5 ) . length )
355+ const discover = {
356+ klass,
357+ methods
358+ } ;
359+
360+ if ( node . id && node . id . name ) {
361+ discover [ 'name' ] = node . id . name ;
362+ }
363+
364+ if ( methods . klass_constructor ) {
365+ discover [ 'attributes' ] = node_loader_trampoline_discover_klass_attributes ( node . body . body )
366+ }
367+ console . log ( discover )
368+
369+ return discover ;
370+ }
371+ }
372+ } catch ( ex ) {
373+ console . log ( `Exception while parsing '${ klass } ' in node_loader_trampoline_discover_klass` , ex ) ;
374+ }
375+ }
376+
377+ function node_loader_trampoline_discover_object ( obj ) {
378+ if ( typeof obj === 'object' ) {
379+ const constructor = ( obj && obj . constructor ) && obj . constructor . name
380+ if ( constructor !== 'Object' && constructor !== 'Array' )
381+ return {
382+ obj
383+ } ;
384+ }
385+ }
386+
296387function node_loader_trampoline_discover ( handle ) {
297388 const discover = { } ;
298389
@@ -305,8 +396,9 @@ function node_loader_trampoline_discover(handle) {
305396
306397 for ( let j = 0 ; j < keys . length ; ++ j ) {
307398 const key = keys [ j ] ;
308- const func = exports [ key ] ;
309- const descriptor = node_loader_trampoline_discover_function ( func ) ;
399+ const value = exports [ key ] ;
400+ const descriptor = node_loader_trampoline_discover_function ( value )
401+ || node_loader_trampoline_discover_klass ( value ) || node_loader_trampoline_discover_object ( value ) ;
310402
311403 if ( descriptor !== undefined ) {
312404 discover [ key ] = descriptor ;
@@ -403,6 +495,8 @@ module.exports = ((impl, ptr) => {
403495 'clear' : node_loader_trampoline_clear ,
404496 'discover' : node_loader_trampoline_discover ,
405497 'discover_function' : node_loader_trampoline_discover_function ,
498+ 'discover_klass' : node_loader_trampoline_discover_klass ,
499+ 'discover_object' : node_loader_trampoline_discover_object ,
406500 'test' : node_loader_trampoline_test ,
407501 'await_function' : node_loader_trampoline_await_function ( trampoline ) ,
408502 'await_future' : node_loader_trampoline_await_future ( trampoline ) ,
0 commit comments