11// This Source Code Form is subject to the terms of the Mozilla Public
22// License, v. 2.0. If a copy of the MPL was not distributed with this
33// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4-
5- // External function declarations for runtime operations
6-
7- // Type definitions
84type ConsoleValue =
95 | string
106 | number
@@ -14,6 +10,25 @@ type ConsoleValue =
1410 | object
1511 | ConsoleValue [ ] ;
1612
13+ // deno-lint-ignore no-explicit-any
14+ const webidl = ( globalThis as any as {
15+ webidl : {
16+ converters : {
17+ any : ( v : unknown ) => unknown ;
18+ boolean : ( v : unknown , prefix ?: string , context ?: string ) => boolean ;
19+ DOMString : ( v : unknown , prefix ?: string , context ?: string ) => string ;
20+ } ;
21+ createSequenceConverter : < T > (
22+ converter : ( v : unknown , prefix ?: string , context ?: string ) => T ,
23+ ) => ( v : unknown , prefix ?: string , context ?: string ) => T [ ] ;
24+ requiredArguments : (
25+ length : number ,
26+ required : number ,
27+ prefix : string ,
28+ ) => void ;
29+ } ;
30+ } ) . webidl ;
31+
1732// ANSI color codes for styling output
1833const COLORS = {
1934 reset : "\x1b[0m" ,
@@ -280,9 +295,9 @@ function createTable(data: ConsoleValue[], headers?: string[]): string {
280295 const table : string [ ] [ ] = [ ] ;
281296 const firstItem = data [ 0 ] ;
282297 const cols = headers ||
283- ( typeof firstItem === "object" && firstItem !== null ?
284- Object . keys ( firstItem as Record < string , ConsoleValue > ) :
285- [ ] ) ;
298+ ( typeof firstItem === "object" && firstItem !== null
299+ ? Object . keys ( firstItem as Record < string , ConsoleValue > )
300+ : [ ] ) ;
286301
287302 // Add header row
288303 table . push ( [ "(index)" , ...cols ] ) ;
@@ -291,9 +306,9 @@ function createTable(data: ConsoleValue[], headers?: string[]): string {
291306 data . forEach ( ( row , index ) => {
292307 const tableRow = [ index . toString ( ) ] ;
293308 cols . forEach ( ( col ) => {
294- const value = row && typeof row === "object" ?
295- ( row as Record < string , ConsoleValue > ) [ col ] :
296- "" ;
309+ const value = row && typeof row === "object"
310+ ? ( row as Record < string , ConsoleValue > ) [ col ]
311+ : "" ;
297312 tableRow . push ( formatValue ( value ) ) ;
298313 } ) ;
299314 table . push ( tableRow ) ;
@@ -403,6 +418,12 @@ const andromedaConsole = {
403418 * ```
404419 */
405420 assert ( condition ?: boolean , ...args : ConsoleValue [ ] ) {
421+ condition = webidl . converters . boolean (
422+ condition ,
423+ "console.assert" ,
424+ "Argument 1" ,
425+ ) ;
426+
406427 if ( condition ) return ; // Early return if condition is true
407428
408429 let message : string ;
@@ -452,8 +473,13 @@ const andromedaConsole = {
452473 * ```
453474 */
454475 count ( label : string = "default" ) {
476+ label = webidl . converters . DOMString (
477+ label ,
478+ "console.count" ,
479+ "Argument 1" ,
480+ ) ;
455481 const message = __andromeda__ . count ( label ) ;
456- console . info ( message ) ;
482+ andromedaConsole . info ( message ) ;
457483 } ,
458484
459485 /**
@@ -466,11 +492,17 @@ const andromedaConsole = {
466492 * ```
467493 */
468494 countReset ( label : string = "default" ) {
495+ label = webidl . converters . DOMString (
496+ label ,
497+ "console.countReset" ,
498+ "Argument 1" ,
499+ ) ;
500+
469501 const message = __andromeda__ . count_reset ( label ) ;
470502 if ( message . includes ( "does not exist" ) ) {
471- console . warn ( message ) ;
503+ andromedaConsole . warn ( message ) ;
472504 } else {
473- console . info ( message ) ;
505+ andromedaConsole . info ( message ) ;
474506 }
475507 } ,
476508
@@ -485,12 +517,14 @@ const andromedaConsole = {
485517 * ```
486518 */
487519 group ( ...args : ConsoleValue [ ] ) {
488- if ( args . length > 0 ) {
489- console . log ( formatArgs ( args ) ) ;
520+ const indent = getIndent ( ) ;
521+ const label = __andromeda__ . group_start (
522+ args . length > 0 ? String ( args [ 0 ] ) : "" ,
523+ ) ;
524+ if ( label ) {
525+ __andromeda__ . internal_print ( `${ indent } ${ label } \n` ) ;
490526 }
491- __andromeda__ . group_start ( args . length > 0 ? String ( args [ 0 ] ) : "" ) ;
492527 } ,
493-
494528 /**
495529 * Creates a new inline group in the console output that is initially collapsed.
496530 * In this implementation, it behaves the same as group().
@@ -530,20 +564,32 @@ const andromedaConsole = {
530564 * ```
531565 */
532566 table ( tabularData ?: ConsoleValue , properties ?: string [ ] ) {
567+ tabularData = webidl . converters . any ( tabularData ) as ConsoleValue ;
568+ if ( properties !== undefined ) {
569+ // Create sequence<DOMString> converter using WebIDL utility
570+ const sequenceConverter = webidl . createSequenceConverter (
571+ webidl . converters . DOMString ,
572+ ) ;
573+ properties = sequenceConverter (
574+ properties ,
575+ "console.table" ,
576+ "Argument 2" ,
577+ ) ;
578+ }
533579 if ( tabularData === null || tabularData === undefined ) {
534- console . log ( tabularData ) ;
580+ andromedaConsole . log ( tabularData ) ;
535581 return ;
536582 }
537583
538584 if ( Array . isArray ( tabularData ) ) {
539- console . log ( createTable ( tabularData , properties ) ) ;
585+ andromedaConsole . log ( createTable ( tabularData , properties ) ) ;
540586 } else if ( typeof tabularData === "object" ) {
541587 const entries = Object . entries (
542588 tabularData as Record < string , ConsoleValue > ,
543589 ) . map ( ( [ key , value ] ) => ( { Key : key , Value : value } ) ) ;
544- console . log ( createTable ( entries ) ) ;
590+ andromedaConsole . log ( createTable ( entries ) ) ;
545591 } else {
546- console . log ( tabularData ) ;
592+ andromedaConsole . log ( tabularData ) ;
547593 }
548594 } ,
549595
@@ -556,9 +602,9 @@ const andromedaConsole = {
556602 * ```
557603 */
558604 dir ( obj ?: ConsoleValue , _options ?: Record < string , ConsoleValue > ) {
559- // Simple implementation - in a full implementation this would show
560- // an expandable object representation
561- console . log ( formatValue ( obj ) ) ;
605+ obj = webidl . converters . any ( obj ) as ConsoleValue ;
606+ // TODO: display an expandable object representation
607+ andromedaConsole . log ( formatValue ( obj ) ) ;
562608 } ,
563609
564610 /**
@@ -571,8 +617,7 @@ const andromedaConsole = {
571617 * ```
572618 */
573619 dirxml ( ...args : ConsoleValue [ ] ) {
574- // For non-browser environments, this behaves like dir()
575- args . forEach ( ( arg ) => console . dir ( arg ) ) ;
620+ andromedaConsole . log ( ...args ) ;
576621 } ,
577622
578623 /**
@@ -587,9 +632,14 @@ const andromedaConsole = {
587632 * ```
588633 */
589634 time ( label : string = "default" ) {
635+ label = webidl . converters . DOMString (
636+ label ,
637+ "console.time" ,
638+ "Argument 1" ,
639+ ) ;
590640 const result = __andromeda__ . time_start ( label ) ;
591641 if ( result ) {
592- console . warn ( result ) ;
642+ andromedaConsole . warn ( result ) ;
593643 }
594644 } ,
595645
@@ -604,14 +654,19 @@ const andromedaConsole = {
604654 * ```
605655 */
606656 timeLog ( label : string = "default" , ...args : ConsoleValue [ ] ) {
657+ label = webidl . converters . DOMString (
658+ label ,
659+ "console.timeLog" ,
660+ "Argument 1" ,
661+ ) ;
607662 const result = __andromeda__ . time_log (
608663 label ,
609664 args . length > 0 ? formatArgs ( args ) : "" ,
610665 ) ;
611666 if ( result . includes ( "does not exist" ) ) {
612- console . warn ( result ) ;
667+ andromedaConsole . warn ( result ) ;
613668 } else {
614- console . info ( result ) ;
669+ andromedaConsole . info ( result ) ;
615670 }
616671 } ,
617672
@@ -627,11 +682,16 @@ const andromedaConsole = {
627682 * ```
628683 */
629684 timeEnd ( label : string = "default" ) {
685+ label = webidl . converters . DOMString (
686+ label ,
687+ "console.timeEnd" ,
688+ "Argument 1" ,
689+ ) ;
630690 const result = __andromeda__ . time_end ( label ) ;
631691 if ( result . includes ( "does not exist" ) ) {
632- console . warn ( result ) ;
692+ andromedaConsole . warn ( result ) ;
633693 } else {
634- console . info ( result ) ;
694+ andromedaConsole . info ( result ) ;
635695 }
636696 } ,
637697
@@ -646,33 +706,32 @@ const andromedaConsole = {
646706 trace ( ...args : ConsoleValue [ ] ) {
647707 const message = args . length > 0 ? formatArgs ( args ) : "Trace" ;
648708 const stack = __andromeda__ . get_stack_trace ( ) ;
649- console . log ( `${ message } \n${ stack } ` ) ;
709+ const indent = getIndent ( ) ;
710+ __andromeda__ . internal_print_err (
711+ `${ COLORS . fg . cyan } ${ indent } ${ message } \n${ stack } ${ COLORS . reset } \n` ,
712+ ) ;
650713 } ,
651714
652715 /**
653- * Starts a profile (no-op in this implementation).
654- * This method exists for compatibility.
716+ * Starts a profile
655717 */
656718 profile ( _label ?: string ) {
657- // No-op in this implementation
719+ // TODO: implement profiling if needed
658720 } ,
659721
660722 /**
661- * Ends a profile (no-op in this implementation).
662- * This method exists for compatibility.
723+ * Ends a profile
663724 */
664725 profileEnd ( _label ?: string ) {
665- // No-op in this implementation
726+ // TODO: implement profiling end if needed
666727 } ,
667728
668729 /**
669- * Adds a timestamp to the console (no-op in this implementation).
670- * This method exists for compatibility.
730+ * Adds a timestamp to the console
671731 */
672732 timeStamp ( _label ?: string ) {
673- // No-op in this implementation
733+ // TODO: implement timestamp if needed
674734 } ,
675735} ;
676736
677- // // Export the console object
678737globalThis . console = andromedaConsole ;
0 commit comments