forked from espruino/Espruino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsflags.c
More file actions
58 lines (52 loc) · 1.46 KB
/
jsflags.c
File metadata and controls
58 lines (52 loc) · 1.46 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
/*
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers
*
* Copyright (C) 2017 Gordon Williams <gw@pur3.co.uk>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* ----------------------------------------------------------------------------
* JS Interpreter specific flags
* ----------------------------------------------------------------------------
*/
#include "jsflags.h"
volatile JsFlags jsFlags;
const char *jsFlagNames = JSFLAG_NAMES;
/// Get the state of a flag
bool jsfGetFlag(JsFlags flag) {
return (jsFlags & flag)!=0;
}
/// Set the state of a flag
void jsfSetFlag(JsFlags flag, bool isOn) {
if (isOn)
jsFlags |= flag;
else
jsFlags &= ~flag;
}
/// Get a list of all flags and their status
JsVar *jsfGetFlags() {
JsVar *o = jsvNewWithFlags(JSV_OBJECT);
if (!o) return 0;
const char *p = jsFlagNames;
JsFlags flag = 1;
while (*p) {
jsvObjectSetChildAndUnLock(o, p, jsvNewFromInteger(jsfGetFlag(flag)?1:0));
p += strlen(p)+1;
flag<<=1;
}
return o;
}
/// Set any of the specified flags
void jsfSetFlags(JsVar *flags) {
if (!jsvIsObject(flags)) return;
const char *p = jsFlagNames;
JsFlags flag = 1;
while (*p) {
JsVar *v = jsvObjectGetChild(flags, p, 0);
if (v) jsfSetFlag(flag, jsvGetBoolAndUnLock(v));
p += strlen(p)+1;
flag<<=1;
}
}