Provide a simpler interface for setting up modules that sets __name__

This commit is contained in:
K. Lange 2023-12-21 10:28:44 +09:00
parent 6ea43c35e0
commit fe430bbb19
12 changed files with 27 additions and 71 deletions

View File

@ -353,3 +353,18 @@ extern int krk_pushStringBuilderFormat(struct StringBuilder * sb, const char * f
extern KrkValue krk_stringFromFormat(const char * fmt, ...);
extern int krk_long_to_int(KrkValue val, char size, void * out);
extern int krk_isSubClass(const KrkClass * cls, const KrkClass * base);
#define KRK_Module_internal_name(name) \
_krk_module_onload_ ## name
#define KRK_Module_internal_sig(name) \
static inline void KRK_Module_internal_name(name) (KrkInstance * module, KrkString * runAs)
#define KRK_Module(name) \
KRK_Module_internal_sig(name); \
KrkValue krk_module_onload_ ## name (KrkString * runAs) { \
KrkInstance * module = krk_newInstance(KRK_BASE_CLASS(module)); \
krk_push(OBJECT_VAL(module)); \
krk_attachNamedObject(&module->fields, "__name__", (KrkObj*)runAs); \
KRK_Module_internal_name(name)(module, runAs); \
return krk_pop(); \
} \
KRK_Module_internal_sig(name)

View File

@ -232,11 +232,8 @@ KRK_Function(examine) {
#endif
KrkValue krk_module_onload_dis(KrkString * runAs) {
KRK_Module(dis) {
#ifndef KRK_DISABLE_DEBUG
KrkInstance * module = krk_newInstance(vm.baseClasses->moduleClass);
krk_push(OBJECT_VAL(module));
KRK_DOC(module,
"@brief Provides tools for disassembling bytecode.\n\n"
"### Code Disassembly in Kuroko\n\n"
@ -341,9 +338,7 @@ KrkValue krk_module_onload_dis(KrkString * runAs) {
"_dis"
);
}
return krk_pop();
#else
return krk_runtimeError(vm.exceptions->notImplementedError, "debugger support is disabled");
krk_runtimeError(vm.exceptions->notImplementedError, "debugger support is disabled");
#endif
}

View File

@ -494,10 +494,7 @@ KRK_Method(Directory,__exit__) {
return FUNC_NAME(Directory,close)(1,argv,0);
}
KrkValue krk_module_onload_fileio(KrkString * runAs) {
KrkInstance * module = krk_newInstance(vm.baseClasses->moduleClass);
krk_push(OBJECT_VAL(module));
KRK_Module(fileio) {
KRK_DOC(module,
"@brief Provides access to C <stdio> buffered file I/O functions.\n\n"
"The @c fileio module provides classes and functions for reading "
@ -571,6 +568,4 @@ KrkValue krk_module_onload_fileio(KrkString * runAs) {
"@arguments path\n\n"
"Opens the directory at @p path and returns a @ref Directory object. If @p path could not be opened or is not "
"a directory, @ref IOError will be raised.");
return krk_pop();
}

View File

@ -19,15 +19,7 @@ KRK_Function(resume) {
return NONE_VAL();
}
KrkValue krk_module_onload_gc(KrkString * runAs) {
/**
* gc = module()
*
* Namespace for methods for controlling the garbage collector.
*/
KrkInstance * module = krk_newInstance(vm.baseClasses->moduleClass);
krk_push(OBJECT_VAL(module));
KRK_Module(gc) {
KRK_DOC(module, "@brief Namespace containing methods for controlling the garbage collector.");
KRK_DOC(BIND_FUNC(module,collect),
@ -36,6 +28,4 @@ KrkValue krk_module_onload_gc(KrkString * runAs) {
"@brief Disables automatic garbage collection until @ref resume is called.");
KRK_DOC(BIND_FUNC(module,resume),
"@brief Re-enable automatic garbage collection after it was stopped by @ref pause ");
return krk_pop();
}

View File

@ -148,10 +148,7 @@ MATH_IS(isnan)
#define bind(name) krk_defineNative(&module->fields, #name, _math_ ## name)
KrkValue krk_module_onload_math(KrkString * runAs) {
KrkInstance * module = krk_newInstance(vm.baseClasses->moduleClass);
krk_push(OBJECT_VAL(module));
KRK_Module(math) {
KRK_DOC(module, "@brief Provides access to floating-point mathematical functions from the system `libm`.");
KRK_DOC(bind(ceil),
"@brief Returns the smallest integer value not less than the input.\n"
@ -271,7 +268,4 @@ KrkValue krk_module_onload_math(KrkString * runAs) {
krk_attachNamedValue(&module->fields, "e", FLOATING_VAL(M_E));
krk_attachNamedValue(&module->fields, "inf", FLOATING_VAL(INFINITY));
krk_attachNamedValue(&module->fields, "nan", FLOATING_VAL(NAN));
krk_pop();
return OBJECT_VAL(module);
}

View File

@ -617,10 +617,7 @@ KRK_Method(stat_result,__repr__) {
return krk_pop();
}
KrkValue krk_module_onload_os(KrkString * runAs) {
KrkInstance * module = krk_newInstance(vm.baseClasses->moduleClass);
krk_push(OBJECT_VAL(module));
KRK_Module(os) {
KRK_DOC(module, "@brief Provides access to low-level system operations.");
#ifdef _WIN32
@ -841,8 +838,6 @@ KrkValue krk_module_onload_os(KrkString * runAs) {
"@brief Get the status of a file\n"
"@arguments path\n\n"
"Runs the @c stat system call on @p path. Returns a @ref stat_result.\n");
return krk_pop();
}

View File

@ -58,16 +58,11 @@ KRK_Function(seed) {
return NONE_VAL();
}
KrkValue krk_module_onload_random(KrkString * runAs) {
KrkInstance * module = krk_newInstance(vm.baseClasses->moduleClass);
krk_push(OBJECT_VAL(module));
KRK_Module(random) {
KRK_DOC(module, "Functions for generating pseudo-random numbers.");
BIND_FUNC(module, random);
BIND_FUNC(module, seed);
FUNC_NAME(krk,seed)(0,NULL,0);
return krk_pop();
}

View File

@ -513,10 +513,7 @@ KRK_Method(socket,proto) {
return INTEGER_VAL(self->proto);
}
KrkValue krk_module_onload_socket(KrkString * runAs) {
KrkInstance * module = krk_newInstance(vm.baseClasses->moduleClass);
krk_push(OBJECT_VAL(module));
KRK_Module(socket) {
KRK_DOC(module, "Lightweight wrapper around the standard Berkeley sockets interface.");
KrkClass * socket = krk_makeClass(module, &SocketClass, "socket", vm.baseClasses->objectClass);
@ -623,6 +620,4 @@ KrkValue krk_module_onload_socket(KrkString * runAs) {
krk_makeClass(module, &SocketError, "SocketError", vm.exceptions->baseException);
KRK_DOC(SocketError, "Raised on faults from socket functions.");
krk_finalizeClass(SocketError);
return krk_pop();
}

View File

@ -40,10 +40,7 @@ KRK_Function(S_ISSOCK) {
}
#endif
KrkValue krk_module_onload_stat(KrkString * runAs) {
KrkInstance * module = krk_newInstance(vm.baseClasses->moduleClass);
krk_push(OBJECT_VAL(module));
KRK_Module(stat) {
KRK_DOC(module, "@brief Functions to check results from @ref stat calls.");
BIND_FUNC(module,S_ISBLK);
@ -55,8 +52,6 @@ KrkValue krk_module_onload_stat(KrkString * runAs) {
BIND_FUNC(module,S_ISLNK);
BIND_FUNC(module,S_ISSOCK);
#endif
return krk_pop();
}

View File

@ -38,9 +38,7 @@ KRK_Function(time) {
return FLOATING_VAL(out);
}
KrkValue krk_module_onload_time(KrkString * runAs) {
KrkInstance * module = krk_newInstance(vm.baseClasses->moduleClass);
krk_push(OBJECT_VAL(module));
KRK_Module(time) {
KRK_DOC(module, "@brief Provides timekeeping functions.");
KRK_DOC(BIND_FUNC(module,sleep), "@brief Pause execution of the current thread.\n"
"@arguments secs\n\n"
@ -50,6 +48,5 @@ KrkValue krk_module_onload_time(KrkString * runAs) {
"Returns a @ref float representation of the number of seconds since the platform's epoch date. "
"On POSIX platforms, this is the number of seconds since 1 January 1970. "
"The precision of the return value is platform-dependent.");
return krk_pop();
}

View File

@ -35,14 +35,8 @@ KRK_Function(timeit) {
return FLOATING_VAL(after-before);
}
KrkValue krk_module_onload_timeit(KrkString * runAs) {
KrkInstance * module = krk_newInstance(vm.baseClasses->moduleClass);
krk_push(OBJECT_VAL(module));
KRK_Module(timeit) {
KRK_DOC(module, "@brief Run functions very quickly without loop overhead from the interpreter.");
BIND_FUNC(module,timeit);
krk_pop();
return OBJECT_VAL(module);
}

View File

@ -17,16 +17,12 @@ KRK_Function(wcwidth) {
return INTEGER_VAL(wcwidth(codepoint));
}
KrkValue krk_module_onload_wcwidth(KrkString * runAs) {
KrkInstance * module = krk_newInstance(vm.baseClasses->moduleClass);
krk_push(OBJECT_VAL(module));
KRK_Module(wcwidth) {
KRK_DOC(module, "Character widths.");
BIND_FUNC(module, wcwidth);
#ifndef _WIN32
setlocale(LC_ALL, "");
#endif
return krk_pop();
}