-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from extism/fix-console
fix: implement console in TS
- Loading branch information
Showing
7 changed files
with
172 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
|
||
declare global { | ||
interface Console { | ||
debug(...data: any[]): void; | ||
error(...data: any[]): void; | ||
info(...data: any[]): void; | ||
log(...data: any[]): void; | ||
warn(...data: any[]): void; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
var __consoleWrite: (level: string, message: string) => void; | ||
} | ||
|
||
function stringifyArg(arg: any): string { | ||
if (arg === null) return 'null'; | ||
if (arg === undefined) return 'undefined'; | ||
|
||
if (typeof arg === 'symbol') return arg.toString(); | ||
if (typeof arg === 'bigint') return `${arg}n`; | ||
if (typeof arg === 'function') return `[Function ${arg.name ? `${arg.name}` : '(anonymous)'}]`; | ||
|
||
if (typeof arg === 'object') { | ||
if (arg instanceof Error) { | ||
return arg.stack || `${arg.name}: ${arg.message}`; | ||
} | ||
if (arg instanceof Set) { | ||
return `Set(${arg.size}) { ${Array.from(arg).map(String).join(', ')} }`; | ||
} | ||
if (arg instanceof Map) { | ||
return `Map(${arg.size}) { ${Array.from(arg).map(([k, v]) => `${k} => ${v}`).join(', ')} }`; | ||
} | ||
if (Array.isArray(arg)) { | ||
const items = []; | ||
for (let i = 0; i < arg.length; i++) { | ||
items.push(i in arg ? stringifyArg(arg[i]) : '<empty>'); | ||
} | ||
return `[ ${items.join(', ')} ]`; | ||
} | ||
|
||
// For regular objects, use JSON.stringify first for clean output | ||
try { | ||
return JSON.stringify(arg); | ||
} catch { | ||
// For objects that can't be JSON stringified (circular refs etc) | ||
// fall back to Object.prototype.toString behavior | ||
return Object.prototype.toString.call(arg); | ||
} | ||
} | ||
|
||
return String(arg); | ||
} | ||
|
||
function createLogFunction(level: string) { | ||
return function (...args: any[]) { | ||
const message = args.map(stringifyArg).join(' '); | ||
__consoleWrite(level, message); | ||
}; | ||
} | ||
|
||
const console = { | ||
trace: createLogFunction('trace'), | ||
debug: createLogFunction('debug'), | ||
log: createLogFunction('info'), | ||
info: createLogFunction('info'), | ||
warn: createLogFunction('warn'), | ||
error: createLogFunction('error'), | ||
}; | ||
|
||
globalThis.console = console; | ||
|
||
export { }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,4 @@ import "./http"; | |
import "./memory"; | ||
import "./memory-handle"; | ||
import "./var"; | ||
import "./console"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"compilerOptions": { | ||
"lib": [], | ||
"types": ["../../crates/core/src/prelude"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
declare module "main" { | ||
export function greet(): I32; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* An example of a plugin that uses Console extensively, CJS flavored plug-in: | ||
*/ | ||
|
||
function greet() { | ||
const n = 1; | ||
tryPrint('n + 1', n + 1); | ||
tryPrint('multiple string args', 'one', 'two', 'three'); | ||
tryPrint('single n', n); | ||
tryPrint('three ns', n, n, n); | ||
tryPrint('n with label', 'n', n); | ||
tryPrint('boolean', true); | ||
tryPrint('null', null); | ||
tryPrint('undefined', undefined); | ||
tryPrint('empty object', {}); | ||
tryPrint('empty array', []); | ||
tryPrint('object with key', { key: 'value' }); | ||
console.warn('This is a warning', 123); | ||
console.error('This is an error', 456); | ||
console.info('This is an info', 789); | ||
console.debug('This is a debug', 101112); | ||
console.trace('This is a trace', 131415); | ||
|
||
console.log('This is an object', { key: 'value' }); | ||
console.log('This is an array', [1, 2, 3]); | ||
console.log('This is a string', 'Hello, World!'); | ||
console.log('This is a number', 123); | ||
console.log('This is a boolean', true); | ||
console.log('This is a null', null); | ||
console.log('This is an undefined', undefined); | ||
console.log('This is a function', function() {}); | ||
console.log('This is a symbol', Symbol('test')); | ||
console.log('This is a date', new Date()); | ||
console.log('This is an error', new Error('Hi there!')); | ||
console.log('This is a map', new Map([[1, 'one'], [2, 'two']] )); | ||
} | ||
|
||
function tryPrint(text, ...args) { | ||
try { | ||
console.log(...args); | ||
console.log(`${text} - ✅`); | ||
} catch (e) { | ||
console.log(`${text} - ❌ - ${e.message}`); | ||
} | ||
console.log('------') | ||
} | ||
|
||
|
||
module.exports = { greet }; |