@@ -336,6 +336,53 @@ static PyObject *context_set_time_limit(ContextData *self, PyObject *args) {
336336Py_RETURN_NONE ;
337337}
338338
339+ // _quickjs.Context.memory
340+ //
341+ // Sets the CPU time limit of the context. This will be used in an interrupt handler.
342+ static PyObject * context_memory (ContextData * self ) {
343+ PyObject * dict = PyDict_New ();
344+ if (dict == NULL ) {
345+ return NULL ;
346+ }
347+ JSMemoryUsage usage ;
348+ JS_ComputeMemoryUsage (self -> runtime , & usage );
349+ #define MEM_USAGE_ADD_TO_DICT (key ) \
350+ { \
351+ PyObject *value = PyLong_FromLongLong(usage.key); \
352+ if (PyDict_SetItemString(dict, #key, value) != 0) { \
353+ return NULL; \
354+ } \
355+ Py_DECREF(value); \
356+ }
357+ MEM_USAGE_ADD_TO_DICT (malloc_size );
358+ MEM_USAGE_ADD_TO_DICT (malloc_limit );
359+ MEM_USAGE_ADD_TO_DICT (memory_used_size );
360+ MEM_USAGE_ADD_TO_DICT (malloc_count );
361+ MEM_USAGE_ADD_TO_DICT (memory_used_count );
362+ MEM_USAGE_ADD_TO_DICT (atom_count );
363+ MEM_USAGE_ADD_TO_DICT (atom_size );
364+ MEM_USAGE_ADD_TO_DICT (str_count );
365+ MEM_USAGE_ADD_TO_DICT (str_size );
366+ MEM_USAGE_ADD_TO_DICT (obj_count );
367+ MEM_USAGE_ADD_TO_DICT (obj_size );
368+ MEM_USAGE_ADD_TO_DICT (prop_count );
369+ MEM_USAGE_ADD_TO_DICT (prop_size );
370+ MEM_USAGE_ADD_TO_DICT (shape_count );
371+ MEM_USAGE_ADD_TO_DICT (shape_size );
372+ MEM_USAGE_ADD_TO_DICT (js_func_count );
373+ MEM_USAGE_ADD_TO_DICT (js_func_size );
374+ MEM_USAGE_ADD_TO_DICT (js_func_code_size );
375+ MEM_USAGE_ADD_TO_DICT (js_func_pc2line_count );
376+ MEM_USAGE_ADD_TO_DICT (js_func_pc2line_size );
377+ MEM_USAGE_ADD_TO_DICT (c_func_count );
378+ MEM_USAGE_ADD_TO_DICT (array_count );
379+ MEM_USAGE_ADD_TO_DICT (fast_array_count );
380+ MEM_USAGE_ADD_TO_DICT (fast_array_elements );
381+ MEM_USAGE_ADD_TO_DICT (binary_object_count );
382+ MEM_USAGE_ADD_TO_DICT (binary_object_size );
383+ return dict ;
384+ }
385+
339386// All methods of the _quickjs.Context class.
340387static PyMethodDef context_methods [] = {
341388 {"eval" , (PyCFunction )context_eval , METH_VARARGS , "Evaluates a Javascript string." },
@@ -348,6 +395,7 @@ static PyMethodDef context_methods[] = {
348395 (PyCFunction )context_set_time_limit ,
349396 METH_VARARGS ,
350397 "Sets the CPU time limit in seconds (C function clock() is used)." },
398+ {"memory" , (PyCFunction )context_memory , METH_NOARGS , "Returns the memory usage as a dict." },
351399 {NULL } /* Sentinel */
352400};
353401
0 commit comments