-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #539 from dictu-lang/feature/object_from
Object.createFrom()
- Loading branch information
Showing
11 changed files
with
183 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--- | ||
layout: default | ||
title: Object | ||
nav_order: 17 | ||
parent: Standard Library | ||
--- | ||
|
||
# Object | ||
{: .no_toc } | ||
|
||
## Table of contents | ||
{: .no_toc .text-delta } | ||
|
||
1. TOC | ||
{:toc} | ||
|
||
--- | ||
|
||
## Object | ||
|
||
To make use of the Object module an import is required. | ||
|
||
```cs | ||
import Object; | ||
``` | ||
|
||
### Object.getClassRef(string) | ||
|
||
This method will attempt to get a class reference from the class name provided as a string. | ||
|
||
Returns a Result and unwraps to an Object upon success. | ||
|
||
**NOTE:** This currently only works if the class is defined in the global scope | ||
|
||
```cs | ||
class Test {} | ||
|
||
Object.getClassRef("Test").unwrap(); // <Cls Test> | ||
``` | ||
|
||
### Object.createFrom(string) | ||
|
||
This method will attempt to create a new object from the class name provided as a string. | ||
|
||
Returns a Result and unwraps to an Object upon success. | ||
|
||
**NOTE:** This currently only works if the class is defined in the global scope | ||
|
||
```cs | ||
class Test {} | ||
|
||
Object.createFrom("Test").unwrap(); // <Test instance> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#define DICTU_OBJECT_SOURCE "import Object;\n" \ | ||
"\n" \ | ||
"def createFrom(className, ...arguments) {\n" \ | ||
" return Object.__getClassRef(className).matchWrap(\n" \ | ||
" def (klass) => klass(...arguments),\n" \ | ||
" def (error) => error\n" \ | ||
" );\n" \ | ||
"}\n" \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include "object.h" | ||
|
||
#include "object-source.h" | ||
|
||
static Value objectGetClassRefImpl(DictuVM *vm, int argCount, Value *args, bool internal) { | ||
if (argCount != 1) { | ||
runtimeError(vm, "getClassRef() takes 1 argument (%d given)", argCount); | ||
return EMPTY_VAL; | ||
} | ||
|
||
if (!IS_STRING(args[0])) { | ||
runtimeError(vm, "getClassRef() argument must be a string"); | ||
return EMPTY_VAL; | ||
} | ||
|
||
ObjString *classString = AS_STRING(args[0]); | ||
|
||
Value klass; | ||
CallFrame *frame; | ||
if (internal) { | ||
// -2 as we want to go to the callee site, not the Object.du code | ||
frame = &vm->frames[vm->frameCount - 2]; | ||
} else { | ||
frame = &vm->frames[vm->frameCount - 1]; | ||
} | ||
|
||
if (tableGet(&frame->closure->function->module->values, classString, &klass) && IS_CLASS(klass)) { | ||
return newResultSuccess(vm, klass); | ||
} | ||
|
||
char *error = ALLOCATE(vm, char, classString->length + 26); | ||
memcpy(error, classString->chars, classString->length); | ||
memcpy(error + classString->length, " class could not be found", 25); | ||
error[classString->length + 25] = '\0'; | ||
|
||
Value result = newResultError(vm, error); | ||
|
||
FREE_ARRAY(vm, char, error, classString->length + 26); | ||
|
||
return result; | ||
} | ||
|
||
static Value objectGetClassRef(DictuVM *vm, int argCount, Value *args) { | ||
return objectGetClassRefImpl(vm, argCount, args, false); | ||
} | ||
|
||
static Value objectGetClassRefInternal(DictuVM *vm, int argCount, Value *args) { | ||
return objectGetClassRefImpl(vm, argCount, args, true); | ||
} | ||
|
||
Value createObjectModule(DictuVM *vm) { | ||
ObjClosure *closure = compileModuleToClosure(vm, "Object", DICTU_OBJECT_SOURCE); | ||
|
||
if (closure == NULL) { | ||
return EMPTY_VAL; | ||
} | ||
|
||
push(vm, OBJ_VAL(closure)); | ||
|
||
/** | ||
* Define Object methods | ||
*/ | ||
defineNative(vm, &closure->function->module->values, "__getClassRef", objectGetClassRefInternal); | ||
defineNative(vm, &closure->function->module->values, "getClassRef", objectGetClassRef); | ||
|
||
pop(vm); | ||
|
||
return OBJ_VAL(closure); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import Object; | ||
|
||
def createFrom(className, ...arguments) { | ||
return Object.__getClassRef(className).matchWrap( | ||
def (klass) => klass(...arguments), | ||
def (error) => error | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#ifndef dictu_object_module_h | ||
#define dictu_object_module_h | ||
|
||
#include <stdlib.h> | ||
|
||
#include "../optionals.h" | ||
#include "../../vm/vm.h" | ||
|
||
Value createObjectModule(DictuVM *vm); | ||
|
||
#endif //dictu_object_module_h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* createFrom.du | ||
* | ||
* Testing the Object.createFrom method | ||
*/ | ||
from UnitTest import UnitTest; | ||
|
||
import Object; | ||
|
||
class Test {} | ||
|
||
class TestObjectCreateFrom < UnitTest { | ||
testObjectCreateFrom() { | ||
this.assertSuccess(Object.createFrom('Test')); | ||
this.assertError(Object.createFrom('Unknown')); | ||
|
||
this.assertType(Object.createFrom('Test').unwrap(), 'Test'); | ||
} | ||
} | ||
|
||
TestObjectCreateFrom().run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* import.du | ||
* | ||
* General import file for all the Object module tests | ||
*/ | ||
|
||
import "createFrom.du"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters