Repository navigation
Expand file tree
/
Copy pathprocess_example.smash
More file actions
96 lines (79 loc) 路 2.5 KB
/
Copy pathprocess_example.smash
File metadata and controls
96 lines (79 loc) 路 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// process_example.smash - Example of using process functionality in SmashLang
import "std";
/**
* Display system information using std process functionality
*/
fn showSystemInfo() {
print("\n=== System Information ===");
print(`Platform: ${std.platform}`);
print(`Architecture: ${std.arch}`);
print(`Process ID: ${std.pid}`);
print(`Parent Process ID: ${std.ppid}`);
// Display environment variables
print("\n=== Environment Variables ===");
print(`HOME: ${std.env.HOME}`);
print(`USER: ${std.env.USER}`);
print(`PATH: ${std.env.PATH ? std.env.PATH.substring(0, 50) + '...' : 'Not available'}`);
print(`SHELL: ${std.env.SHELL}`);
// Display current working directory
print("\n=== Working Directory ===");
print(`Current directory: ${std.cwd()}`);
// Display memory usage
print("\n=== Memory Usage ===");
const memory = std.memoryUsage();
print(`RSS: ${formatBytes(memory.rss)}`);
print(`Heap Total: ${formatBytes(memory.heapTotal)}`);
print(`Heap Used: ${formatBytes(memory.heapUsed)}`);
print(`External: ${formatBytes(memory.external)}`);
// Display command line arguments
print("\n=== Command Line Arguments ===");
print("Arguments passed to this script:");
for (let i = 2; i < std.argv.length; i++) {
print(` [${i-2}]: ${std.argv[i]}`);
}
}
/**
* Format bytes to a human-readable string
* @param {number} bytes - Number of bytes
* @returns {string} - Formatted string
*/
fn formatBytes(bytes) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
/**
* Demonstrate event handling
*/
fn setupEventHandlers() {
// Set up exit handler
std.on('exit', (code) => {
print(`\nProcess is exiting with code: ${code}`);
});
// Set up uncaught exception handler
std.on('uncaughtException', (error) => {
print(`\nUncaught exception: ${error}`);
});
}
/**
* Main function
*/
fn main() {
print("SmashLang Process Example");
print("==========================");
// Set up event handlers
setupEventHandlers();
// Show system information
showSystemInfo();
// Check if we should exit
if (std.argv.includes('--exit')) {
const exitCode = parseInt(std.argv[std.argv.indexOf('--exit') + 1] || '0');
print(`\nExiting with code ${exitCode}...`);
std.exit(exitCode);
}
print("\nExample completed!");
}
// Run the main function
main();