-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Object.createFrom() #539
Merged
Merged
Object.createFrom() #539
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8181821
Add new Object module and createFrom method
Jason2605 fed6402
Wrap object in result type and handle case where class can't be found
Jason2605 3ebf812
Add getClassRef and write createFrom in Dictu
Jason2605 4d78c12
Merge branch 'feature/unpack' into feature/object_from
Jason2605 da7e494
Merge branch 'develop' into feature/object_from
Jason2605 4f4bf14
Use the correct unpack operator
Jason2605 e22b050
Merge branch 'develop' into feature/object_from
Jason2605 f3f7a79
Correct documentation for Object module
Jason2605 9b77aed
Add to test suite
Jason2605 d177afa
Add note to Object module
Jason2605 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there an easy way to get the name of the class from an object? I know we have a getAttributes method but I don't believe it includes the name of the class. That might be a nice addition so this can be even more dynamic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you say class name, do you mean an actual reference to the class or do you mean the string representation of the class name?
For example, if you already have the object you can get the class reference via: https://dictu-lang.com/docs/classes/#_class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The string representation of the class name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah there's currently no way to turn a class reference into a string'd name. We could add a
_name
attribute similar to_class
to classesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be cool.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be added as part of #543
Now it would be:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very cool!