|
@@ -8,6 +8,8 @@
|
|
|
|
|
|
#include <inttypes.h>
|
|
#include <inttypes.h>
|
|
#include <math.h>
|
|
#include <math.h>
|
|
|
|
+#include <time.h>
|
|
|
|
+#include <stdlib.h>
|
|
#include "gravity_core.h"
|
|
#include "gravity_core.h"
|
|
#include "gravity_hash.h"
|
|
#include "gravity_hash.h"
|
|
#include "gravity_value.h"
|
|
#include "gravity_value.h"
|
|
@@ -1793,6 +1795,37 @@ static bool system_nanotime (gravity_vm *vm, gravity_value_t *args, uint16_t nar
|
|
RETURN_VALUE(VALUE_FROM_INT(t), rindex);
|
|
RETURN_VALUE(VALUE_FROM_INT(t), rindex);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+static bool system_random (gravity_vm *vm, gravity_value_t *args, uint16_t nargs, uint32_t rindex) {
|
|
|
|
+ #pragma unused(args)
|
|
|
|
+ if (nargs != 3) RETURN_ERROR("System.random() expects 2 integer arguments");
|
|
|
|
+
|
|
|
|
+ if (!VALUE_ISA_INT(GET_VALUE(1)) || !VALUE_ISA_INT(GET_VALUE(2))) RETURN_ERROR("System.random() arguments must be integers");
|
|
|
|
+
|
|
|
|
+ gravity_int_t num1 = VALUE_AS_INT(GET_VALUE(1));
|
|
|
|
+ gravity_int_t num2 = VALUE_AS_INT(GET_VALUE(2));
|
|
|
|
+
|
|
|
|
+ // Only Seed once
|
|
|
|
+ static bool already_seeded = false;
|
|
|
|
+ if (!already_seeded) {
|
|
|
|
+ srand(time(NULL));
|
|
|
|
+ already_seeded = true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ int r;
|
|
|
|
+ // if num1 is lower, consider it min, otherwise, num2 is min
|
|
|
|
+ if (num1 < num2) {
|
|
|
|
+ // returns a random integer between num1 and num2 inclusive
|
|
|
|
+ r = (rand() % (num2 - num1 + 1)) + num1;
|
|
|
|
+ }
|
|
|
|
+ else if (num1 > num2) {
|
|
|
|
+ r = (rand() % (num1 - num2 + 1)) + num2;
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ r = num1;
|
|
|
|
+ }
|
|
|
|
+ RETURN_VALUE(VALUE_FROM_INT(r), rindex);
|
|
|
|
+}
|
|
|
|
+
|
|
static bool system_realprint (gravity_vm *vm, gravity_value_t *args, uint16_t nargs, uint32_t rindex, bool cr) {
|
|
static bool system_realprint (gravity_vm *vm, gravity_value_t *args, uint16_t nargs, uint32_t rindex, bool cr) {
|
|
#pragma unused (rindex)
|
|
#pragma unused (rindex)
|
|
for (uint16_t i=1; i<nargs; ++i) {
|
|
for (uint16_t i=1; i<nargs; ++i) {
|
|
@@ -2076,6 +2109,7 @@ static void gravity_core_init (void) {
|
|
gravity_class_bind(system_meta, GRAVITY_SYSTEM_NANOTIME_NAME, NEW_CLOSURE_VALUE(system_nanotime));
|
|
gravity_class_bind(system_meta, GRAVITY_SYSTEM_NANOTIME_NAME, NEW_CLOSURE_VALUE(system_nanotime));
|
|
gravity_class_bind(system_meta, GRAVITY_SYSTEM_PRINT_NAME, NEW_CLOSURE_VALUE(system_print));
|
|
gravity_class_bind(system_meta, GRAVITY_SYSTEM_PRINT_NAME, NEW_CLOSURE_VALUE(system_print));
|
|
gravity_class_bind(system_meta, GRAVITY_SYSTEM_PUT_NAME, NEW_CLOSURE_VALUE(system_put));
|
|
gravity_class_bind(system_meta, GRAVITY_SYSTEM_PUT_NAME, NEW_CLOSURE_VALUE(system_put));
|
|
|
|
+ gravity_class_bind(system_meta, "random", NEW_CLOSURE_VALUE(system_random));
|
|
gravity_class_bind(system_meta, "exit", NEW_CLOSURE_VALUE(system_exit));
|
|
gravity_class_bind(system_meta, "exit", NEW_CLOSURE_VALUE(system_exit));
|
|
|
|
|
|
gravity_value_t value = VALUE_FROM_OBJECT(computed_property(NULL, NEW_FUNCTION(system_get), NEW_FUNCTION(system_set)));
|
|
gravity_value_t value = VALUE_FROM_OBJECT(computed_property(NULL, NEW_FUNCTION(system_get), NEW_FUNCTION(system_set)));
|