]> gitweb.factorcode.org Git - factor.git/commitdiff
VM: refactoring to remove redundancy in retainstack|datastack_to_array
authorBjörn Lindqvist <bjourne@gmail.com>
Sat, 28 Feb 2015 19:41:32 +0000 (19:41 +0000)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sat, 28 Feb 2015 22:14:30 +0000 (14:14 -0800)
vm/contexts.cpp
vm/vm.hpp

index 59625ca28ce2328110c197acde7e3077adf0394e..0f6c455156438611be9aa927a6430f3e51c0d77f 100644 (file)
@@ -174,26 +174,22 @@ void factor_vm::primitive_context_object_for() {
 }
 
 /* Allocates memory */
-cell factor_vm::stack_to_array(cell bottom, cell top) {
+cell factor_vm::stack_to_array(cell bottom, cell top, vm_error_type error) {
   fixnum depth = (fixnum)(top - bottom + sizeof(cell));
 
-  if (depth < 0)
-    return false_object;
-  else {
-    array* a = allot_uninitialized_array<array>(depth / sizeof(cell));
-    memcpy(a + 1, (void*)bottom, depth);
-    return tag<array>(a);
+  if (depth < 0) {
+    general_error(error, false_object, false_object);
   }
+  array* a = allot_uninitialized_array<array>(depth / sizeof(cell));
+  memcpy(a + 1, (void*)bottom, depth);
+  return tag<array>(a);
 }
 
 /* Allocates memory */
 cell factor_vm::datastack_to_array(context* ctx) {
-  cell array = stack_to_array(ctx->datastack_seg->start, ctx->datastack);
-  if (array == false_object) {
-    general_error(ERROR_DATASTACK_UNDERFLOW, false_object, false_object);
-    return false_object;
-  } else
-    return array;
+  return stack_to_array(ctx->datastack_seg->start,
+                        ctx->datastack,
+                        ERROR_DATASTACK_UNDERFLOW);
 }
 
 /* Allocates memory */
@@ -207,12 +203,9 @@ void factor_vm::primitive_datastack_for() {
 
 /* Allocates memory */
 cell factor_vm::retainstack_to_array(context* ctx) {
-  cell array = stack_to_array(ctx->retainstack_seg->start, ctx->retainstack);
-  if (array == false_object) {
-    general_error(ERROR_RETAINSTACK_UNDERFLOW, false_object, false_object);
-    return false_object;
-  } else
-    return array;
+  return stack_to_array(ctx->retainstack_seg->start,
+                        ctx->retainstack,
+                        ERROR_RETAINSTACK_UNDERFLOW);
 }
 
 /* Allocates memory */
index ba688671fbe46b4e72de24350eaf48ef040f1d8e..a6988c2de5440ecfaa5f9586be346b95b21f8a56 100644 (file)
--- a/vm/vm.hpp
+++ b/vm/vm.hpp
@@ -164,7 +164,7 @@ struct factor_vm {
   void primitive_context_object();
   void primitive_context_object_for();
   void primitive_set_context_object();
-  cell stack_to_array(cell bottom, cell top);
+  cell stack_to_array(cell bottom, cell top, vm_error_type error);
   cell datastack_to_array(context* ctx);
   void primitive_datastack();
   void primitive_datastack_for();