Skip to content

Commit 1b46300

Browse files
committed
Add _quickjs.Context.memory function.
1 parent a6a287b commit 1b46300

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

.clang-format

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
BasedOnStyle: Google
22

33
# For calculating line lengths.
4-
IndentWidth: 2
5-
TabWidth: 2
4+
IndentWidth: 4
5+
TabWidth: 4
66

77
UseTab: ForIndentation
88
ColumnLimit: 100

module.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,53 @@ static PyObject *context_set_time_limit(ContextData *self, PyObject *args) {
336336
Py_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.
340387
static 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

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def get_c_sources(include_headers=False):
2727
author_email="petter.strandmark@gmail.com",
2828
name='quickjs',
2929
url='https://github.com/PetterS/quickjs',
30-
version='1.1.2',
30+
version='1.2.0',
3131
description='Wrapping the quickjs C library.',
3232
long_description=long_description,
3333
packages=["quickjs"],

test_quickjs.py

100755100644
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ def test_time_limit(self):
106106
self.context.set_time_limit(-1)
107107
self.context.eval(code)
108108

109+
def test_memory_usage(self):
110+
self.assertIn("memory_used_size", self.context.memory().keys())
111+
109112

110113
class Object(unittest.TestCase):
111114
def setUp(self):

0 commit comments

Comments
 (0)