Skip to content
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

Importlib #763

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -477,4 +477,4 @@ If the given index + byte length does exceed the buffer bounds an error is retur
const buffer = Buffer.new(8).unwrap();
buffer.writeDoubleBE(0, 14.34);
print(buffer.readDoubleBE(0).unwrap()) // 14.34
```
```
47 changes: 47 additions & 0 deletions docs/docs/standard-lib/importlib.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
layout: default
title: Importlib
nav_order: 10
parent: Standard Library
---

# Inspect
{: .no_toc }

## Table of contents
{: .no_toc .text-delta }

1. TOC
{:toc}

---

## Importlib
To make use of the Importlib module an import is required.

```js
import Importlib;
```

### Importlib.include(String, Bool: reload -> Optional) -> Module

This allows you to dynamically import a module. It also allows you to reload a module that was previously imported.

Note: The path is relative to the file include() is called in.
Note: Reloading an already imported module will mutate all references to the previously imported module.

```js
// someFile.du
class SomeClass {
hello() {
print("Hello!");
}
}
```

```js
// main.du
const SomeModule = Importlib.include('someFile.du'); // <Module someFile.du>

SomeModule.SomeClass().hello();
```
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/inspect.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Inspect
nav_order: 10
nav_order: 11
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/io.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: IO
nav_order: 11
nav_order: 12
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/json.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: JSON
nav_order: 12
nav_order: 13
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/log.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Log
nav_order: 13
nav_order: 14
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/math.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Math
nav_order: 14
nav_order: 15
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/net.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Net
nav_order: 15
nav_order: 16
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/object.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Object
nav_order: 16
nav_order: 17
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/path.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Path
nav_order: 17
nav_order: 18
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/process.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Process
nav_order: 18
nav_order: 19
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/queue.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Queue
nav_order: 19
nav_order: 20
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/random.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Random
nav_order: 20
nav_order: 21
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/socket.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Socket
nav_order: 21
nav_order: 22
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/sqlite.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Sqlite
nav_order: 22
nav_order: 23
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/stack.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Stack
nav_order: 23
nav_order: 24
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/system.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: System
nav_order: 24
nav_order: 25
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/term.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Term
nav_order: 25
nav_order: 26
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/unittest.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: UnitTest
nav_order: 26
nav_order: 27
parent: Standard Library
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/standard-lib/uuid.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: UUID
nav_order: 27
nav_order: 28
parent: Standard Library
---

Expand Down
3 changes: 3 additions & 0 deletions ops/checkTests.du
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ const ignored = {
'range.du',
'select.du',
],
'importlib': [
'data'
],
'*': [
'import.du',
]
Expand Down
82 changes: 82 additions & 0 deletions src/optionals/importlib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "importlib.h"

Value includeNative(DictuVM *vm, int argCount, Value *args) {
if (argCount != 1 && argCount != 2) {
runtimeError(vm, "include() takes 1 or 2 arguments (%d given)", argCount);
return EMPTY_VAL;
}

if (!IS_STRING(args[0])) {
runtimeError(vm, "include() first argument must be a string");
return EMPTY_VAL;
}

bool reload = false;

if (argCount == 2) {
if (!IS_BOOL(args[1])) {
runtimeError(vm, "include() second argument must be a boolean");
return EMPTY_VAL;
}

reload = AS_BOOL(args[1]);
}

CallFrame *frame = &vm->frames[vm->frameCount - 1];
char path[PATH_MAX];

if (!resolvePath(frame->closure->function->module->path->chars, AS_CSTRING(args[0]), path)) {
runtimeError(vm, "Could not open file \"%s\".", AS_CSTRING(args[0]));
return EMPTY_VAL;
}

Value moduleVal;
ObjString *pathObj = copyString(vm, path, strlen(path));
if (!reload && tableGet(&vm->modules, pathObj, &moduleVal)) {
return moduleVal;
}

push(vm, OBJ_VAL(pathObj));
ObjModule *module = newModule(vm, pathObj);
pop(vm);

push(vm, OBJ_VAL(module));
module->path = dirname(vm, path, strlen(path));

char *source = readFile(vm, path);

if (source == NULL) {
runtimeError(vm, "Could not open file \"%s\".", AS_CSTRING(args[0]));
return EMPTY_VAL;
}

ObjFunction *function = compile(vm, module, source);
push(vm, OBJ_VAL(function));
FREE_ARRAY(vm, char, source, strlen(source) + 1);

if (function == NULL) {
return EMPTY_VAL;
}

ObjClosure *closure = newClosure(vm, function);
pop(vm);

callFunction(vm, OBJ_VAL(closure), 0, NULL);
pop(vm);

return OBJ_VAL(module);
}

Value createImportlibModule(DictuVM *vm) {
ObjString *name = copyString(vm, "Importlib", 9);
push(vm, OBJ_VAL(name));
ObjModule *module = newModule(vm, name);
push(vm, OBJ_VAL(module));

defineNative(vm, &module->values, "include", includeNative);

pop(vm);
pop(vm);

return OBJ_VAL(module);
}
11 changes: 11 additions & 0 deletions src/optionals/importlib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef dictu_importlib_h
#define dictu_importlib_h

#include "optionals.h"
#include "../vm/common.h"
#include "../vm/vm.h"
#include "../vm/memory.h"

Value createImportlibModule(DictuVM *vm);

#endif //dictu_importlib_h
1 change: 1 addition & 0 deletions src/optionals/optionals.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ BuiltinModules modules[] = {
{"BigInt", &createBigIntModule, false},
{"Buffer", &createBufferModule, false},
{"FFI", &createFFIModule, false},
{"Importlib", &createImportlibModule, false},
{NULL, NULL, false}
};

Expand Down
1 change: 1 addition & 0 deletions src/optionals/optionals.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "buffer.h"
#include "unittest/unittest.h"
#include "ffi.h"
#include "importlib.h"

typedef Value (*BuiltinModule)(DictuVM *vm);

Expand Down
12 changes: 6 additions & 6 deletions src/optionals/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ static Value getgidNative(DictuVM *vm, int argCount, Value *args) {
UNUSED(args);

if (argCount != 0) {
runtimeError(vm, "getgid() doesn't take any argument (%d given)", argCount);
runtimeError(vm, "getgid() doesn't take any arguments (%d given)", argCount);
return EMPTY_VAL;
}

Expand All @@ -24,7 +24,7 @@ static Value getegidNative(DictuVM *vm, int argCount, Value *args) {
UNUSED(args);

if (argCount != 0) {
runtimeError(vm, "getegid() doesn't take any argument (%d given)", argCount);
runtimeError(vm, "getegid() doesn't take any arguments (%d given)", argCount);
return EMPTY_VAL;
}

Expand All @@ -35,7 +35,7 @@ static Value getuidNative(DictuVM *vm, int argCount, Value *args) {
UNUSED(args);

if (argCount != 0) {
runtimeError(vm, "getuid() doesn't take any argument (%d given)", argCount);
runtimeError(vm, "getuid() doesn't take any arguments (%d given)", argCount);
return EMPTY_VAL;
}

Expand All @@ -46,7 +46,7 @@ static Value geteuidNative(DictuVM *vm, int argCount, Value *args) {
UNUSED(args);

if (argCount != 0) {
runtimeError(vm, "geteuid() doesn't take any argument (%d given)", argCount);
runtimeError(vm, "geteuid() doesn't take any arguments (%d given)", argCount);
return EMPTY_VAL;
}

Expand All @@ -57,7 +57,7 @@ static Value getppidNative(DictuVM *vm, int argCount, Value *args) {
UNUSED(args);

if (argCount != 0) {
runtimeError(vm, "getppid() doesn't take any argument (%d given)", argCount);
runtimeError(vm, "getppid() doesn't take any arguments (%d given)", argCount);
return EMPTY_VAL;
}

Expand All @@ -68,7 +68,7 @@ static Value getpidNative(DictuVM *vm, int argCount, Value *args) {
UNUSED(args);

if (argCount != 0) {
runtimeError(vm, "getpid() doesn't take any argument (%d given)", argCount);
runtimeError(vm, "getpid() doesn't take any arguments (%d given)", argCount);
return EMPTY_VAL;
}

Expand Down
Loading
Loading