Skip to content

Commit

Permalink
Renamed optional classes and fixed a setter unwanted side effect.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcobambini committed Sep 7, 2019
1 parent 3a61227 commit 7b9284e
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 56 deletions.
36 changes: 18 additions & 18 deletions gravity.xcodeproj/project.pbxproj
Expand Up @@ -28,9 +28,9 @@
A9506D5C1E69AB8D009A0045 /* gravity_debug.c in Sources */ = {isa = PBXBuildFile; fileRef = A9506D2A1E69AB1E009A0045 /* gravity_debug.c */; };
A9506D5D1E69AB8D009A0045 /* gravity_json.c in Sources */ = {isa = PBXBuildFile; fileRef = A9506D2C1E69AB1E009A0045 /* gravity_json.c */; };
A9506D5E1E69AB8D009A0045 /* gravity_utils.c in Sources */ = {isa = PBXBuildFile; fileRef = A9506D2E1E69AB1E009A0045 /* gravity_utils.c */; };
A956AADE220EBB8100C361D6 /* gravity_env.c in Sources */ = {isa = PBXBuildFile; fileRef = A956AADC220EBB8100C361D6 /* gravity_env.c */; };
A989830623189DFF006F82BE /* gravity_json.c in Sources */ = {isa = PBXBuildFile; fileRef = A989830423189DFF006F82BE /* gravity_json.c */; };
A9BB72F01F470832002FD2D6 /* gravity_math.c in Sources */ = {isa = PBXBuildFile; fileRef = A9BB72EC1F470832002FD2D6 /* gravity_math.c */; };
A956AADE220EBB8100C361D6 /* gravity_opt_env.c in Sources */ = {isa = PBXBuildFile; fileRef = A956AADC220EBB8100C361D6 /* gravity_opt_env.c */; };
A989830623189DFF006F82BE /* gravity_opt_json.c in Sources */ = {isa = PBXBuildFile; fileRef = A989830423189DFF006F82BE /* gravity_opt_json.c */; };
A9BB72F01F470832002FD2D6 /* gravity_opt_math.c in Sources */ = {isa = PBXBuildFile; fileRef = A9BB72EC1F470832002FD2D6 /* gravity_opt_math.c */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -94,12 +94,12 @@
A9506D2D1E69AB1E009A0045 /* gravity_json.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = gravity_json.h; sourceTree = "<group>"; };
A9506D2E1E69AB1E009A0045 /* gravity_utils.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = gravity_utils.c; sourceTree = "<group>"; };
A9506D2F1E69AB1E009A0045 /* gravity_utils.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = gravity_utils.h; sourceTree = "<group>"; };
A956AADC220EBB8100C361D6 /* gravity_env.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = gravity_env.c; sourceTree = "<group>"; };
A956AADD220EBB8100C361D6 /* gravity_env.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = gravity_env.h; sourceTree = "<group>"; };
A989830423189DFF006F82BE /* gravity_json.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = gravity_json.c; sourceTree = "<group>"; };
A989830523189DFF006F82BE /* gravity_json.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = gravity_json.h; sourceTree = "<group>"; };
A9BB72EC1F470832002FD2D6 /* gravity_math.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = gravity_math.c; sourceTree = "<group>"; };
A9BB72ED1F470832002FD2D6 /* gravity_math.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = gravity_math.h; sourceTree = "<group>"; };
A956AADC220EBB8100C361D6 /* gravity_opt_env.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = gravity_opt_env.c; sourceTree = "<group>"; };
A956AADD220EBB8100C361D6 /* gravity_opt_env.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = gravity_opt_env.h; sourceTree = "<group>"; };
A989830423189DFF006F82BE /* gravity_opt_json.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = gravity_opt_json.c; sourceTree = "<group>"; };
A989830523189DFF006F82BE /* gravity_opt_json.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = gravity_opt_json.h; sourceTree = "<group>"; };
A9BB72EC1F470832002FD2D6 /* gravity_opt_math.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = gravity_opt_math.c; sourceTree = "<group>"; };
A9BB72ED1F470832002FD2D6 /* gravity_opt_math.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = gravity_opt_math.h; sourceTree = "<group>"; };
A9BB72EE1F470832002FD2D6 /* gravity_optionals.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = gravity_optionals.h; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -229,12 +229,12 @@
A9BB72EB1F470832002FD2D6 /* optionals */ = {
isa = PBXGroup;
children = (
A9BB72EC1F470832002FD2D6 /* gravity_math.c */,
A9BB72ED1F470832002FD2D6 /* gravity_math.h */,
A989830423189DFF006F82BE /* gravity_json.c */,
A989830523189DFF006F82BE /* gravity_json.h */,
A956AADC220EBB8100C361D6 /* gravity_env.c */,
A956AADD220EBB8100C361D6 /* gravity_env.h */,
A9BB72EC1F470832002FD2D6 /* gravity_opt_math.c */,
A9BB72ED1F470832002FD2D6 /* gravity_opt_math.h */,
A989830423189DFF006F82BE /* gravity_opt_json.c */,
A989830523189DFF006F82BE /* gravity_opt_json.h */,
A956AADC220EBB8100C361D6 /* gravity_opt_env.c */,
A956AADD220EBB8100C361D6 /* gravity_opt_env.h */,
A9BB72EE1F470832002FD2D6 /* gravity_optionals.h */,
);
path = optionals;
Expand Down Expand Up @@ -304,7 +304,7 @@
A9506D571E69AB86009A0045 /* gravity_memory.c in Sources */,
A9506D581E69AB86009A0045 /* gravity_value.c in Sources */,
A9506D3B1E69AB78009A0045 /* gravity_codegen.c in Sources */,
A956AADE220EBB8100C361D6 /* gravity_env.c in Sources */,
A956AADE220EBB8100C361D6 /* gravity_opt_env.c in Sources */,
A9506D5D1E69AB8D009A0045 /* gravity_json.c in Sources */,
A9506D531E69AB7E009A0045 /* gravity_vm.c in Sources */,
A9506D411E69AB78009A0045 /* gravity_semacheck1.c in Sources */,
Expand All @@ -315,12 +315,12 @@
A9506D421E69AB78009A0045 /* gravity_semacheck2.c in Sources */,
A9506D3E1E69AB78009A0045 /* gravity_lexer.c in Sources */,
A9506D431E69AB78009A0045 /* gravity_symboltable.c in Sources */,
A9BB72F01F470832002FD2D6 /* gravity_math.c in Sources */,
A9BB72F01F470832002FD2D6 /* gravity_opt_math.c in Sources */,
A9506D3A1E69AB78009A0045 /* gravity_ast.c in Sources */,
A9506D5E1E69AB8D009A0045 /* gravity_utils.c in Sources */,
A9506D451E69AB78009A0045 /* gravity_visitor.c in Sources */,
A9506D3C1E69AB78009A0045 /* gravity_compiler.c in Sources */,
A989830623189DFF006F82BE /* gravity_json.c in Sources */,
A989830623189DFF006F82BE /* gravity_opt_json.c in Sources */,
A9506D521E69AB7E009A0045 /* gravity_core.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
6 changes: 3 additions & 3 deletions src/cli/gravity.c
Expand Up @@ -170,11 +170,11 @@ static const char *unittest_read (const char *path, size_t *size, uint32_t *file
static void unittest_scan (const char *folder_path, unittest_data *data) {
DIRREF dir = directory_init(folder_path);
if (!dir) return;
#ifdef WIN32
#ifdef WIN32
char outbuffer[MAX_PATH];
#else
#else
char * outbuffer = NULL;
#endif
#endif
const char *target_file;
while ((target_file = directory_read(dir, outbuffer))) {

Expand Down
12 changes: 6 additions & 6 deletions src/compiler/gravity_codegen.c
Expand Up @@ -1060,14 +1060,14 @@ static void visit_variable_decl (gvisitor_t *self, gnode_variable_decl_t *node)
// example 1:
// var a;
//
// LOADK 0 NULL ; move null into register 0
// STOREG 0 0 ; move register 0 into hash(constant_pool(0))
// LOADK 0 NULL ; move null into register 0
// STOREG 0 0 ; move register 0 into hash(constant_pool(0))
//
// example 2:
// var a = 10;
//
// LOADI 0 10 ; move 10 into register 0
// STOREG 0 0 ; move register 0 into hash(constant_pool(0))
// LOADI 0 10 ; move 10 into register 0
// STOREG 0 0 ; move register 0 into hash(constant_pool(0))
//

gravity_function_t *context_function = (gravity_function_t *)context_object;
Expand Down Expand Up @@ -1119,8 +1119,8 @@ static void visit_variable_decl (gvisitor_t *self, gnode_variable_decl_t *node)
// depending if variable has been created static
// and push it as current declaration then:
//
// LOADI 0 10 ; move 10 into register 0
// STOREF 0 0 ; move register 0 into property 0
// LOADI 0 10 ; move 10 into register 0
// STOREF 0 0 ; move register 0 into property 0

if (p->expr) {
// was gravity_class_lookup but that means than $init or init will be recursively searched also in super classes
Expand Down
Expand Up @@ -17,7 +17,7 @@
#include "gravity_opcodes.h"
#include "gravity_debug.h"

#include "gravity_env.h"
#include "gravity_opt_env.h"

#if defined(_WIN32)
#define setenv(_key, _value_, _unused) _putenv_s(_key, _value_)
Expand Down
File renamed without changes.
@@ -1,21 +1,21 @@
//
// gravity_json.c
// gravity_opt_json.c
// gravity
//
// Created by Marco Bambini on 20/08/2019.
// Copyright © 2019 CreoLabs. All rights reserved.
//

#include <inttypes.h>
#include "gravity_json.h"
#include "gravity_vm.h"
#include "gravity_json.h"
#include "gravity_core.h"
#include "gravity_hash.h"
#include "gravity_json.h"
#include "gravity_utils.h"
#include "gravity_macros.h"
#include "gravity_opcodes.h"
#include "gravity_vmmacros.h"
#include "gravity_opt_json.h"

#define GRAVITY_JSON_STRINGIFY_NAME "stringify"
#define GRAVITY_JSON_PARSE_NAME "parse"
Expand Down
@@ -1,5 +1,5 @@
//
// gravity_json.h
// gravity_opt_json.h
// gravity
//
// Created by Marco Bambini on 20/08/2019.
Expand Down
@@ -1,5 +1,5 @@
//
// gravity_math.c
// gravity_opt_math.c
// gravity
//
// Created by Marco Bambini on 14/08/2017.
Expand All @@ -11,12 +11,12 @@
#include <time.h>
#include <inttypes.h>
#include "gravity_vm.h"
#include "gravity_math.h"
#include "gravity_core.h"
#include "gravity_hash.h"
#include "gravity_utils.h"
#include "gravity_macros.h"
#include "gravity_vmmacros.h"
#include "gravity_opt_math.h"

#if GRAVITY_ENABLE_DOUBLE
#define SIN sin
Expand Down
@@ -1,5 +1,5 @@
//
// gravity_math.h
// gravity_opt_math.h
// gravity
//
// Created by Marco Bambini on 14/08/2017.
Expand Down
6 changes: 3 additions & 3 deletions src/optionals/gravity_optionals.h
Expand Up @@ -18,7 +18,7 @@
#define GRAVITY_MATH_FREE() gravity_math_free()
#define GRAVITY_MATH_NAME() gravity_math_name()
#define GRAVITY_ISMATH_CLASS(_c) gravity_ismath_class(_c)
#include "gravity_math.h"
#include "gravity_opt_math.h"
#else
#define GRAVITY_MATH_REGISTER(_vm)
#define GRAVITY_MATH_FREE()
Expand All @@ -35,7 +35,7 @@
#define GRAVITY_JSON_FREE() gravity_json_free()
#define GRAVITY_JSON_NAME() gravity_json_name()
#define GRAVITY_ISJSON_CLASS(_c) gravity_isjson_class(_c)
#include "gravity_json.h"
#include "gravity_opt_json.h"
#else
#define GRAVITY_JSON_REGISTER(_vm)
#define GRAVITY_JSON_FREE()
Expand All @@ -52,7 +52,7 @@
#define GRAVITY_ENV_FREE() gravity_env_free()
#define GRAVITY_ENV_NAME() gravity_env_name()
#define GRAVITY_ISENV_CLASS(_c) gravity_isenv_class(_c)
#include "gravity_env.h"
#include "gravity_opt_env.h"
#else
#define GRAVITY_ENV_REGISTER(_vm)
#define GRAVITY_ENV_FREE()
Expand Down
6 changes: 4 additions & 2 deletions src/runtime/gravity_vm.c
Expand Up @@ -7,6 +7,7 @@
//

#include "gravity_hash.h"
#include "gravity_json.h"
#include "gravity_array.h"
#include "gravity_debug.h"
#include "gravity_macros.h"
Expand All @@ -15,7 +16,6 @@
#include "gravity_opcodes.h"
#include "gravity_memory.h"
#include "gravity_vmmacros.h"
#include "gravity_json.h"
#include "gravity_optionals.h"

// MARK: Internals -
Expand Down Expand Up @@ -588,7 +588,9 @@ static bool gravity_vm_exec (gravity_vm *vm) {
// invalidate current_fiber because it does not need to be in sync in this case
current_fiber = NULL;
if (reset_r1) SETVALUE(rwin+1, v1);
PUSH_FRAME(closure, &stackstart[rwin], r1, 2);
// was r1 but it was incorrect, pass r3 as the destination register because I am sure it is a
// dummy temp (and unused) register that can be safely set to NULL
PUSH_FRAME(closure, &stackstart[rwin], r3, 2);
} break;

case EXEC_TYPE_INTERNAL: {
Expand Down
4 changes: 2 additions & 2 deletions src/shared/gravity_value.h
Expand Up @@ -66,8 +66,8 @@
extern "C" {
#endif

#define GRAVITY_VERSION "0.7.5" // git tag 0.7.5
#define GRAVITY_VERSION_NUMBER 0x000705 // git push --tags
#define GRAVITY_VERSION "0.7.6" // git tag 0.7.6
#define GRAVITY_VERSION_NUMBER 0x000706 // git push --tags
#define GRAVITY_BUILD_DATE __DATE__

#ifndef GRAVITY_ENABLE_DOUBLE
Expand Down
4 changes: 2 additions & 2 deletions src/utils/gravity_debug.c
Expand Up @@ -41,7 +41,7 @@ const char *opcode_name (opcode_t op) {
bindex += snprintf(&buffer[bindex], balloc-bindex, __VA_ARGS__); \
bindex += snprintf(&buffer[bindex], balloc-bindex, "\n");

#define DUMP_VM_NOCR(buffer, bindex, ...) bindex += snprintf(&buffer[bindex], balloc-bindex, "%06u\t", pc); \
#define DUMP_VM_NOCR(buffer, bindex, ...) bindex += snprintf(&buffer[bindex], balloc-bindex, "%06u\t", pc); \
bindex += snprintf(&buffer[bindex], balloc-bindex, __VA_ARGS__);

#define DUMP_VM_RAW(buffer, bindex, ...) bindex += snprintf(&buffer[bindex], balloc-bindex, __VA_ARGS__);
Expand All @@ -51,7 +51,7 @@ const char *gravity_disassemble (gravity_vm *vm, gravity_function_t *f, const ch
uint32_t pc = 0, inst = 0, ninsts = 0;
opcode_t op;

const int rowlen = 256;
const int rowlen = 256;
uint32_t bindex = 0;
uint32_t balloc = 0;
char *buffer = NULL;
Expand Down
2 changes: 1 addition & 1 deletion test/unittest/classes/base_object_methods.gravity
Expand Up @@ -32,7 +32,7 @@ func main() {

r = v.String(); if (r != v) return "-7 (" + r + ")";
r = bar.String(); if (r != "bar") return "-9 (" + r + ")";
r = 0.1.String(); if (r != "0.100000") return "-10 (" + r + ")";
r = 0.1.String(); if (r != "0.1") return "-10 (" + r + ")";
r = null.String(); if (r != "null") return "-11 (" + r + ")";
r = true.String(); if (r != "true") return "-12 (" + r + ")";
r = main.String(); if (r != "main") return "-13 (" + r + ")";
Expand Down
20 changes: 10 additions & 10 deletions test/unittest/conversion/conversion_string.gravity
Expand Up @@ -8,24 +8,24 @@ func main() {
var v,r;

v = "str";
r = v.String(); if (r != "str") return "-1 (" + r + ")";
r = "str".String(); if (r != "str") return "-2 (" + r + ")";
r = v.String(); if (r != "str") return "-1 (" + r + ")";
r = "str".String(); if (r != "str") return "-2 (" + r + ")";

v = null;
r = v.String(); if (r != "null") return "-3 (" + r + ")";
r = null.String(); if (r != "null") return "-4 (" + r + ")";
r = v.String(); if (r != "null") return "-3 (" + r + ")";
r = null.String(); if (r != "null") return "-4 (" + r + ")";

v = true;
r = v.String(); if (r != "true") return "-5 (" + r + ")";
r = true.String(); if (r != "true") return "-6 (" + r + ")";
r = v.String(); if (r != "true") return "-5 (" + r + ")";
r = true.String(); if (r != "true") return "-6 (" + r + ")";

v = 6;
r = v.String(); if (r != "6") return "-7 (" + r + ")";
r = 6.String(); if (r != "6") return "-8 (" + r + ")";
r = v.String(); if (r != "6") return "-7 (" + r + ")";
r = 6.String(); if (r != "6") return "-8 (" + r + ")";

v = 1.1;
r = v.String(); if (r != "1.100000") return "-9 (" + r + ")";
r = 1.1.String(); if (r != "1.100000") return "-10 (" + r + ")";
r = v.String(); if (r != "1.1") return "-9 (" + r + ")";
r = 1.1.String(); if (r != "1.1") return "-10 (" + r + ")";

return true;
}
2 changes: 1 addition & 1 deletion test/unittest/float_literals.gravity
@@ -1,6 +1,6 @@
#unittest {
name: "Float complex literals";
result: "[21.120120,12.200100,0.122120,0.000000]";
result: "[21.1201,12.2001,0.12212,0]";
};

func main() {
Expand Down
33 changes: 33 additions & 0 deletions test/unittest/setter_sideeffect.gravity
@@ -0,0 +1,33 @@
#unittest {
name: "Prevents setter side effects.";
result: "This is test string1.This is test string2.";
};

class SetterTest {
private var _member1;
private var _member2;

var member1 {
set {
_member1 = value;
}
}

var member2 {
set {
_member2 = value;
return value;
}
}

}

func main() {
var str1 = "This is test string1.";
var str2 = "This is test string2.";

var setter_test = SetterTest();
setter_test.member1 = str1;
setter_test.member2 = str2;
return str1 + str2;
}

0 comments on commit 7b9284e

Please sign in to comment.