Jump to content

Host introspection: Difference between revisions

Added Wren
(Changed the way to compute the word size using "sizeof(int)".)
(Added Wren)
Line 1,171:
 
No match, so big endian.
 
=={{header|Wren}}==
{{trans|C}}
As this information cannot be reliably obtained via Wren CLI, we instead embed a Wren script in a C application and ask the host program to get it for us.
<lang ecmascript>/* host_introspection.wren */
 
class C {
foreign static wordSize
foreign static endianness
}
 
System.print("word size = %(C.wordSize) bits")
System.print("endianness = %(C.endianness)")</lang>
<br>
We now embed this Wren script in the following C program, compile and run it.
<lang c>#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include "wren.h"
 
void C_wordSize(WrenVM* vm) {
/* size_t typically is exactly one word */
int ws = (int)(CHAR_BIT * sizeof(size_t));
 
/* return result to Wren */
wrenSetSlotDouble(vm, 0, (double)ws);
}
 
void C_endianness(WrenVM* vm) {
/* Check if the least significant bit is located in the lowest-address byte. */
int one = 1;
char *e = (*(char *)&one) ? "little" : "big";
 
/* return result to Wren */
wrenSetSlotString(vm, 0, e);
}
 
WrenForeignMethodFn bindForeignMethod(
WrenVM* vm,
const char* module,
const char* className,
bool isStatic,
const char* signature) {
if (strcmp(module, "main") == 0) {
if (strcmp(className, "C") == 0) {
if (isStatic && strcmp(signature, "wordSize") == 0) {
return C_wordSize;
} else if (isStatic && strcmp(signature, "endianness") == 0) {
return C_endianness;
}
}
}
return NULL;
}
 
static void writeFn(WrenVM* vm, const char* text) {
printf("%s", text);
}
 
void errorFn(WrenVM* vm, WrenErrorType errorType, const char* module, const int line, const char* msg) {
switch (errorType) {
case WREN_ERROR_COMPILE:
printf("[%s line %d] [Error] %s\n", module, line, msg);
break;
case WREN_ERROR_STACK_TRACE:
printf("[%s line %d] in %s\n", module, line, msg);
break;
case WREN_ERROR_RUNTIME:
printf("[Runtime Error] %s\n", msg);
break;
}
}
 
char *readFile(const char *fileName) {
FILE *f = fopen(fileName, "r");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
rewind(f);
char *script = malloc(fsize + 1);
fread(script, 1, fsize, f);
fclose(f);
script[fsize] = 0;
return script;
}
 
int main() {
WrenConfiguration config;
wrenInitConfiguration(&config);
config.writeFn = &writeFn;
config.errorFn = &errorFn;
config.bindForeignMethodFn = &bindForeignMethod;
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "host_introspection.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
switch (result) {
case WREN_RESULT_COMPILE_ERROR:
printf("Compile Error!\n");
break;
case WREN_RESULT_RUNTIME_ERROR:
printf("Runtime Error!\n");
break;
case WREN_RESULT_SUCCESS:
break;
}
wrenFreeVM(vm);
free(script);
return 0;
}</lang>
 
{{out}}
The results, as expected, for my x64 Ubuntu 20.04 system are:
<pre>
word size = 64 bits
endianness = little
</pre>
 
=={{header|XPL0}}==
9,483

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.