1 module janet.vm;
2 
3 import janet.c;
4 
5 import janet;
6 
7 /// Run a string in the Janet VM.
8 @nogc int doString(string str,JanetTable* env = coreEnv)
9 {
10     import std.string : representation;
11     return janet_dobytes(env,&str.representation[0],cast(int)str.length,"",null);
12 }
13 /// ditto 
14 @nogc int doString(string str,Janet* out_,JanetTable* env = coreEnv)
15 {
16     import std.string : representation;
17     return janet_dobytes(env,&str.representation[0],cast(int)str.length,"",out_);
18 }
19 ///
20 unittest
21 {
22     doString(`(print "doString unittest succeeded!")`);
23 }
24 
25 /// Load a file and run it in the Janet VM.
26 @nogc int doFile(string path,JanetTable* env = coreEnv)
27 {
28     import std.io.file : File;
29     import core.memory : pureMalloc, pureFree;
30     int len = 0;
31     ubyte* buffer = cast(ubyte*)pureMalloc(ubyte.sizeof * 0x10_0000);
32     scope(exit) pureFree(buffer);
33     synchronized
34     {
35         auto f = File(path);
36         len = cast(int)f.read(buffer[0..0x10_0000]);
37     }
38     return janet_dobytes(env,cast(ubyte*)buffer,len,"",null);
39 }
40 /// ditto
41 @nogc int doFile(string path, Janet* out_, JanetTable* env = coreEnv)
42 {
43     import std.io.file : File;
44     import core.memory : pureMalloc, pureFree;
45     int len = 0;
46     ubyte* buffer = cast(ubyte*)pureMalloc(ubyte.sizeof * 0x10_0000);
47     scope(exit) pureFree(buffer);
48     synchronized
49     {
50         auto f = File(path);
51         len = cast(int)f.read(buffer[0..0x10_0000]);
52     }
53     return janet_dobytes(env,cast(ubyte*)buffer,len,"",out_);
54 }
55 
56 unittest
57 {
58     import std.parallelism;
59     import std.stdio;
60     TaskPool ourPool = new TaskPool();
61     writeln("Testing parallelism...");
62     import std.file : readText;
63     string memoizedString = readText("./source/tests/dtests/parallel.janet");
64     foreach(int i;0..100_000)
65     {
66         if(i%10_000==0)
67         {
68             memoizedString = readText("./source/tests/dtests/parallel.janet");
69         }
70         ourPool.put(task!doString(memoizedString));
71     }
72     ourPool.finish();
73     writeln("Parallelism test finished.");
74 }