Remove libraries from root scope (fix #463)

This commit is contained in:
Gordon Williams 2014-12-11 15:00:01 +00:00
parent 2aa649c3b4
commit acb068e965
5 changed files with 29 additions and 21 deletions

View File

@ -17,6 +17,7 @@
Only keep RTC settings if the relevant oscillator is running (fix #444)
Finally fixed sporadic compilation problems with '-Os'
Fixed issues with intervals in onInit (#462)
Remove libraries from root scope (fix #463)
1v71 : Allowed WIZnet + CC3000 to be instantiated on any pins
Fix break inside loop inside case inside function (fix 428)
@ -58,7 +59,7 @@
Fix issue where large doubles (> +/- 2^31) were converted to -1 rather than truncated ints
Fix E.getSizeOf (fix #424)
Fixed JSON indentation issue
Made 'pretty' JSON output look a bit better
Made 'pretty' JSON output look a bit better
1v69 : Fix 1v67's regression of digitalPulse when doing lots of pulses
Add configurable OneWire search command (for finding DS18B20s with alarm set)

View File

@ -66,8 +66,10 @@ bool jsfsInit() {
/* TODO: maybe this should be in the 'E' library. However we don't currently
* have a way of doing that in build_jswrapper.py */
/*JSON{
"type" : "library",
"type" : "class",
"class" : "File"
}
This is the File object - it allows you to stream data to and from files (As opposed to the `require('fs').readFile(..)` style functions that read an entire file).

View File

@ -134,6 +134,9 @@ def codeOutSymbolTable(builtin):
strLen = 0
for sym in builtin["functions"]:
symName = sym["name"];
if builtin["name"]=="global" and symName in libraries:
continue # don't include libraries on global namespace
if "generate" in sym:
listSymbols.append("{"+", ".join([str(strLen), "(void (*)(void))"+sym["generate"], getArgumentSpecifier(sym)])+"}")
listChars = listChars + symName + "\\0";
@ -264,6 +267,13 @@ codeOut('// --------------------------------------------------------------------
codeOut('');
codeOut('');
print "Finding Libraries"
libraries = []
for jsondata in jsondatas:
if jsondata["type"]=="library":
print "Found library "+jsondata["class"]
libraries.append(jsondata["class"])
print "Classifying Functions"
builtins = {}
for jsondata in jsondatas:
@ -414,18 +424,11 @@ codeOut('}')
codeOut('')
codeOut('')
builtinLibraryChecks = []
for jsondata in jsondatas:
if jsondata["type"]=="library":
check = 'strcmp(name, "'+jsondata["class"]+'")==0';
builtinLibraryChecks.append(check)
builtinChecks = []
for jsondata in jsondatas:
if "class" in jsondata:
check = 'strcmp(name, "'+jsondata["class"]+'")==0';
if not check in builtinLibraryChecks:
if not jsondata["class"] in libraries:
if not check in builtinChecks:
builtinChecks.append(check)
@ -438,11 +441,10 @@ codeOut('')
codeOut('')
codeOut('bool jswIsBuiltInLibrary(const char *name) {')
if len(builtinLibraryChecks)==0:
codeOut(' return false;')
else:
codeOut(' return\n'+" ||\n ".join(builtinLibraryChecks)+';')
codeOut('void *jswGetBuiltInLibrary(const char *name) {')
for lib in libraries:
codeOut('if (strcmp(name, "'+lib+'")==0) return (void*)gen_jswrap_'+lib+'_'+lib+';');
codeOut(' return 0;')
codeOut('}')
codeOut('')

View File

@ -67,9 +67,10 @@ JsVar *jswrap_require(JsVar *moduleName) {
// Now check if it is built-in
char moduleNameBuf[32];
jsvGetString(moduleName, moduleNameBuf, sizeof(moduleNameBuf));
if (jswIsBuiltInLibrary(moduleNameBuf)) {
void *builtInLib = jswGetBuiltInLibrary(moduleNameBuf);
if (builtInLib) {
// create a 'fake' module that Espruino can use to map its built-in functions against
moduleExport = jspNewBuiltin(moduleNameBuf);
moduleExport = jsvNewNativeFunction(builtInLib, 0);
} else {
// Now try and load it
JsVar *fileContents = 0;
@ -93,8 +94,8 @@ JsVar *jswrap_require(JsVar *moduleName) {
jsvUnLock(fileContents);
}
assert(moduleExport);
jsvSetValueOfName(moduleExportName, moduleExport); // save in cache
if (moduleExport) // could have been out of memory
jsvSetValueOfName(moduleExportName, moduleExport); // save in cache
jsvUnLock(moduleExportName);
return moduleExport;
}

View File

@ -70,8 +70,10 @@ const JswSymList *jswGetSymbolListForObjectProto(JsVar *parent);
/// Given the name of an Object, see if we should set it up as a builtin or not
bool jswIsBuiltInObject(const char *name);
/// If we get this in 'require', should we make an object with this name?
bool jswIsBuiltInLibrary(const char *name);
/** If we get this in 'require', do we have the object for this
inside the interpreter already? If so, return the native function
pointer of the object's constructor */
void *jswGetBuiltInLibrary(const char *name);
/** Given a variable, return the basic object name of it */
const char *jswGetBasicObjectName(JsVar *var);