-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathos_native.c
More file actions
115 lines (97 loc) · 3.26 KB
/
os_native.c
File metadata and controls
115 lines (97 loc) · 3.26 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// --------------------------------------------------
// Project: ProX Programming Language (ProXPL)
// Author: ProgrammerKR
// Created: 2025-12-25
// Copyright © 2025. ProXentix India Pvt. Ltd. All rights reserved.
// --------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#define PLATFORM_NAME "windows"
#define POPEN _popen
#define PCLOSE _pclose
#else
#include <unistd.h>
#include <sys/utsname.h>
#define PLATFORM_NAME "linux" // or macos detect
#define POPEN popen
#define PCLOSE pclose
#endif
#include "../../include/common.h"
#include "../../include/vm.h"
#include "../../include/value.h"
#include "../../include/object.h"
extern VM vm;
// Helper
static void defineModuleFn(ObjModule* module, const char* name, NativeFn function) {
ObjString* nameObj = copyString(name, (int)strlen(name));
push(&vm, OBJ_VAL(nameObj));
push(&vm, OBJ_VAL(newNative(function)));
tableSet(&module->exports, nameObj, peek(&vm, 0));
pop(&vm);
pop(&vm);
}
static void defineModuleConst(ObjModule* module, const char* name, Value value) {
ObjString* nameObj = copyString(name, (int)strlen(name));
push(&vm, OBJ_VAL(nameObj));
push(&vm, value);
tableSet(&module->exports, nameObj, value);
pop(&vm);
pop(&vm);
}
// platform() -> String
static Value native_platform(int argCount, Value* args) {
return OBJ_VAL(copyString(PLATFORM_NAME, (int)strlen(PLATFORM_NAME)));
}
// cpu_count() -> Number
static Value native_cpu_count(int argCount, Value* args) {
#ifdef _WIN32
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return NUMBER_VAL((double)sysinfo.dwNumberOfProcessors);
#else
return NUMBER_VAL((double)sysconf(_SC_NPROCESSORS_ONLN));
#endif
}
// exec(cmd) -> String (Output)
static Value native_exec(int argCount, Value* args) {
if (argCount < 1 || !IS_STRING(args[0])) return NIL_VAL;
const char* cmd = AS_CSTRING(args[0]);
FILE* pipe = POPEN(cmd, "r");
if (!pipe) return NIL_VAL;
char buffer[128];
// Read output mostly to string. Limitation: large output.
// For now, read first 1024 bytes or so? Or realloc.
// Let's implement simple reader.
// NOTE: Implementing full dynamic string builder here is tedious without helper.
// Sticking to fixed buffer for MVP.
char result[4096];
result[0] = '\0';
size_t currentLen = 0;
while (fgets(buffer, sizeof(buffer), pipe) != NULL) {
size_t len = strlen(buffer);
if (currentLen + len < sizeof(result)) {
strcat(result, buffer);
currentLen += len;
} else {
break; // Truncated
}
}
PCLOSE(pipe);
return OBJ_VAL(copyString(result, (int)currentLen));
}
ObjModule* create_std_os_module() {
ObjString* name = copyString("std.native.os", 14);
push(&vm, OBJ_VAL(name));
ObjModule* module = newModule(name);
push(&vm, OBJ_VAL(module));
defineModuleFn(module, "platform", native_platform);
defineModuleFn(module, "cpu_count", native_cpu_count);
defineModuleFn(module, "exec", native_exec);
defineModuleConst(module, "PLATFORM", OBJ_VAL(copyString(PLATFORM_NAME, strlen(PLATFORM_NAME))));
pop(&vm);
pop(&vm);
return module;
}