forked from gfwilliams/tiny-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTinyJS_Additional.cpp
More file actions
86 lines (75 loc) · 2.45 KB
/
Copy pathTinyJS_Additional.cpp
File metadata and controls
86 lines (75 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "TinyJS_Additional.h"
/* These are my added functions for things that seem like
they should be in the standard javascript, are mentioned
in the Fusion manual, yet, don't exist. If / when I verify
that they are standard javascript and what the behavior
should be, I will move them to the main body of JS functions.
Jared McLaughlin ( tinyJS-F360Post ) 21 Oct 2023
*/
//CScriptVar shortcut macro
#define scIsInt(a) ( c->getParameter(a)->isInt() )
#define scIsDouble(a) ( c->getParameter(a)->isDouble() )
#define scIsObject(a) ( c->getParameter(a)->isObject() )
#define scGetInt(a) ( c->getParameter(a)->getInt() )
#define scGetDouble(a) ( c->getParameter(a)->getDouble() )
#define scGetString(a) ( c->getParameter(a)->getString() )
#define scReturnInt(a) ( c->getReturnVar()->setInt(a) )
#define scReturnDouble(a) ( c->getReturnVar()->setDouble(a) )
#define scReturnString(a) ( c->getReturnVar()->setString(a) )
// we're never writing to more than one output file at a time
// this allows our writeln function to keep the same signature
int fd=-1;
int myopen(const char* filename, int amode, int option)
{
#ifdef linux
if (option != 0) {
return open(filename, amode, option);
}
else {
return open(filename, amode);
}
#else
char work[1024];
strcpy(work, filename);
int ptr = 0;
while (work[ptr]) {
if (work[ptr] == '/') {
work[ptr] = '\\';
}
ptr++;
}
if (option != 0) {
return _open(work, amode, option);
}
else {
return _open(work, amode);
}
#endif
}
void fileOpen(char *filename){
#ifdef linux
fd=myopen("out.nc",O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE );
#else
fd=myopen("out.nc",O_CREAT | O_TRUNC | O_RDWR | O_BINARY, S_IREAD | S_IWRITE );
#endif
if(fd<0) throw new CScriptException("Error opening file.");
}
// one at a time!
void fileClose() {
close(fd);
}
void js_writeln(CScriptVar* c, void* userdata) {
if(fd<0) {
throw new CScriptException("No output file open.");
}
const char *source=scGetString("text").c_str();
int len=strlen(source);
char string[len+1];
strcpy(string,source);
strcat(string,"\n");
if(write(fd,string,len+1)<0)
throw new CScriptException("Error writing to file.");
}
void registerAdditionalFunctions(CTinyJS* tinyJS) {
tinyJS->addNative("function writeln(text)", js_writeln, 0);
}