@@ -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+
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,91 @@ 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+ const node = ( ast . body [ 0 ] . type === 'ExpressionStatement' ) && ast . body [ 0 ] . expression ;
349+ if ( node . type === 'ClassExpression' ) {
350+ const methods = node_loader_trampoline_discover_klass_methods ( node . body . body , str )
351+ const discover = {
352+ klass,
353+ methods
354+ } ;
355+
356+ if ( node . id && node . id . name ) {
357+ discover [ 'name' ] = node . id . name ;
358+ }
359+
360+ if ( methods . klass_constructor ) {
361+ discover [ 'attributes' ] = node_loader_trampoline_discover_klass_attributes ( node . body . body ) ;
362+ }
363+
364+ return discover ;
365+ }
366+ }
367+ } catch ( ex ) {
368+ console . log ( `Exception while parsing '${ klass } ' in node_loader_trampoline_discover_klass` , ex ) ;
369+ }
370+ }
371+
372+ function node_loader_trampoline_discover_object ( obj ) {
373+ if ( typeof obj === 'object' ) {
374+ const constructor = ( obj && obj . constructor ) && obj . constructor . name
375+ if ( constructor !== 'Object' && constructor !== 'Array' )
376+ return {
377+ obj
378+ } ;
379+ }
380+ }
381+
296382function node_loader_trampoline_discover ( handle ) {
297383 const discover = { } ;
298384
@@ -305,8 +391,9 @@ function node_loader_trampoline_discover(handle) {
305391
306392 for ( let j = 0 ; j < keys . length ; ++ j ) {
307393 const key = keys [ j ] ;
308- const func = exports [ key ] ;
309- const descriptor = node_loader_trampoline_discover_function ( func ) ;
394+ const value = exports [ key ] ;
395+ const descriptor = node_loader_trampoline_discover_function ( value )
396+ || node_loader_trampoline_discover_klass ( value ) || node_loader_trampoline_discover_object ( value ) ;
310397
311398 if ( descriptor !== undefined ) {
312399 discover [ key ] = descriptor ;
@@ -411,6 +498,8 @@ module.exports = ((impl, ptr) => {
411498 'clear' : node_loader_trampoline_clear ,
412499 'discover' : node_loader_trampoline_discover ,
413500 'discover_function' : node_loader_trampoline_discover_function ,
501+ 'discover_klass' : node_loader_trampoline_discover_klass ,
502+ 'discover_object' : node_loader_trampoline_discover_object ,
414503 'test' : node_loader_trampoline_test ,
415504 'await_function' : node_loader_trampoline_await_function ( trampoline ) ,
416505 'await_future' : node_loader_trampoline_await_future ( trampoline ) ,
0 commit comments