1212var microbitFsWrapper = function ( ) {
1313 var fsWrapper = { } ;
1414
15- var fs1 = null ;
16- var fs2 = null ;
17-
15+ var uPyFs = null ;
1816 var commonFsSize = 20 * 1024 ;
17+ var passthroughMethods = [
18+ 'create' ,
19+ 'exists' ,
20+ 'getStorageRemaining' ,
21+ 'getStorageSize' ,
22+ 'getUniversalHex' ,
23+ 'ls' ,
24+ 'read' ,
25+ 'readBytes' ,
26+ 'remove' ,
27+ 'size' ,
28+ 'write' ,
29+ ] ;
1930
2031 /**
21- * Looks up the object keys from the MicropythonFsHex prototype and
22- * creates functions with the same name in this object to run the same
23- * method in both instances of MicropythonFsHex()
32+ * Duplicates some of the methods from the MicropythonFsHex class by
33+ * creating functions with the same name in this object.
2434 */
25- function duplicateKeys ( ) {
26- // Only start the function duplication once both instances have been created
27- Object . keys ( Object . getPrototypeOf ( fs1 ) ) . forEach ( function ( key ) {
28- // We will duplicate all functions to call both instances
29- if ( typeof fs1 [ key ] === 'function' ) {
30- fsWrapper [ key ] = function ( ) {
31- var return1 = fs1 [ key ] . apply ( fs1 , arguments ) ;
32- var return2 = fs2 [ key ] . apply ( fs2 , arguments ) ;
33- // FIXME: Keep this during general testing, probably remove on final release for speed
34- if ( JSON . stringify ( return1 ) !== JSON . stringify ( return2 ) ) {
35- console . error ( 'Return from call to ' + key + ' differs:\n\t' + return1 + '\n\t' + return2 ) ;
36- }
37- return return1 ;
38- } ;
39- }
35+ function duplicateMethods ( ) {
36+ passthroughMethods . forEach ( function ( method ) {
37+ fsWrapper [ method ] = function ( ) {
38+ return uPyFs [ method ] . apply ( uPyFs , arguments ) ;
39+ } ;
4040 } ) ;
41- // Remove the MicropythonFsHex functions we don't want to expose
42- delete fsWrapper [ 'getIntelHex' ] ;
43- delete fsWrapper [ 'getIntelHexBytes' ] ;
4441 }
4542
4643 /**
4744 * Fetches both MicroPython hexes and sets up the file system with the
4845 * initial main.py
4946 */
5047 fsWrapper . setupFilesystem = function ( ) {
48+ var uPyV1 = null ;
49+ var uPyV2 = null ;
50+
5151 var deferred1 = $ . get ( 'micropython/microbit-micropython-v1.hex' , function ( fileStr ) {
52- fs1 = new microbitFs . MicropythonFsHex ( fileStr , {
53- 'maxFsSize' : commonFsSize ,
54- } ) ;
52+ uPyV1 = fileStr ;
5553 } ) . fail ( function ( ) {
56- console . error ( 'Could not load the MicroPython hex 1 file.' ) ;
54+ console . error ( 'Could not load the MicroPython v1 file.' ) ;
5755 } ) ;
5856 var deferred2 = $ . get ( 'micropython/microbit-micropython-v2.hex' , function ( fileStr ) {
59- fs2 = new microbitFs . MicropythonFsHex ( fileStr , {
60- 'maxFsSize' : commonFsSize ,
61- } ) ;
57+ uPyV2 = fileStr ;
6258 } ) . fail ( function ( ) {
63- console . error ( 'Could not load the MicroPython hex 2 file.' ) ;
59+ console . error ( 'Could not load the MicroPython v2 file.' ) ;
6460 } ) ;
6561
6662 return $ . when ( deferred1 , deferred2 ) . done ( function ( ) {
67- duplicateKeys ( ) ;
63+ if ( ! uPyV1 || ! uPyV2 ) {
64+ console . error ( 'There was an issue loading the MicroPython Hex files.' ) ;
65+ }
66+ uPyFs = new microbitFs . MicropythonFsHex ( [
67+ { hex : uPyV1 , boardId : 0x9900 } ,
68+ { hex : uPyV2 , boardId : 0x9903 } ,
69+ ] , {
70+ 'maxFsSize' : commonFsSize ,
71+ } ) ;
72+ duplicateMethods ( ) ;
6873 } ) ;
6974 } ;
7075
71- /**
72- * @returns {string } Universal Hex string.
73- */
74- fsWrapper . getUniversalHex = function ( ) {
75- return microbitUh . createUniversalHex ( [
76- { 'hex' : fs1 . getIntelHex ( ) , 'boardId' : 0x9900 } ,
77- { 'hex' : fs2 . getIntelHex ( ) , 'boardId' : 0x9903 } ,
78- ] ) ;
79- } ;
80-
8176 /**
8277 * @param {string } boardId String with the Board ID for the generation.
8378 * @returns Uint8Array with the data for the given Board ID.
8479 */
8580 fsWrapper . getBytesForBoardId = function ( boardId ) {
8681 if ( boardId == '9900' || boardId == '9901' ) {
87- return fs1 . getIntelHexBytes ( ) ;
82+ return uPyFs . getIntelHexBytes ( 0x9900 ) ;
8883 } else if ( boardId == '9903' || boardId == '9904' ) {
89- return fs2 . getIntelHexBytes ( ) ;
84+ return uPyFs . getIntelHexBytes ( 0x9903 ) ;
9085 } else {
9186 throw Error ( 'Could not recognise the Board ID ' + boardId ) ;
9287 }
@@ -98,9 +93,9 @@ var microbitFsWrapper = function() {
9893 */
9994 fsWrapper . getIntelHexForBoardId = function ( boardId ) {
10095 if ( boardId == '9900' || boardId == '9901' ) {
101- var hexStr = fs1 . getIntelHex ( ) ;
96+ var hexStr = uPyFs . getIntelHex ( 0x9900 ) ;
10297 } else if ( boardId == '9903' || boardId == '9904' ) {
103- var hexStr = fs2 . getIntelHex ( )
98+ var hexStr = uPyFs . getIntelHex ( 0x9903 ) ;
10499 } else {
105100 throw Error ( 'Could not recognise the Board ID ' + boardId ) ;
106101 }
@@ -115,67 +110,39 @@ var microbitFsWrapper = function() {
115110 /**
116111 * Import the files from the provide hex string into the filesystem.
117112 * If the import is successful this deletes all the previous files.
118- *
113+ *
119114 * @param {string } hexStr Hex (Intel or Universal) string with files to
120- * import.
115+ * import.
121116 * @return {string[] } Array with the filenames of all files imported.
122117 */
123- fsWrapper . importFiles = function ( hexStr ) {
124- var hex = '' ;
125- if ( microbitUh . isUniversalHex ( hexStr ) ) {
126- // For now only extract one of the file systems
127- microbitUh . separateUniversalHex ( hexStr ) . forEach ( function ( hexObj ) {
128- if ( hexObj . boardId == 0x9900 || hexObj . boardId == 0x9901 ) {
129- hex = hexObj . hex ;
130- }
131- } ) ;
132- if ( ! hex ) {
133- // TODO: Add this string to the language files
134- throw new Error ( 'Universal Hex does not contain data for the supported boards.' ) ;
135- }
136- } else {
137- hex = hexStr ;
138- }
139-
140- // TODO: Add this string to the language files
141- var errorMsg = 'Not a Universal Hex\n' ;
142- try {
143- var filesNames1 = fs1 . importFilesFromIntelHex ( hex , {
144- overwrite : true ,
145- formatFirst : true
146- } ) ;
147- var filesNames2 = fs2 . importFilesFromIntelHex ( hex , {
148- overwrite : true ,
149- formatFirst : true
150- } ) ;
151- // FIXME: Keep this during general testing, probably remove on final release for speed
152- if ( JSON . stringify ( filesNames1 ) !== JSON . stringify ( filesNames2 ) ) {
153- console . error ( 'Return from importFilesFromIntelHex() differs:' +
154- '\n\t' + return1 + '\n\t' + return2 ) ;
155- }
156- return filesNames1 ;
157- } catch ( e ) {
158- errorMsg += e . message + '\n' ;
118+ fsWrapper . importHexFiles = function ( hexStr ) {
119+ var filesNames = uPyFs . importFilesFromHex ( hexStr , {
120+ overwrite : true ,
121+ formatFirst : true
122+ } ) ;
123+ if ( ! filesNames . length ) {
124+ throw new Error ( 'The filesystem in the hex file was empty' ) ;
159125 }
126+ return filesNames ;
127+ } ;
160128
161- // Failed during fs file import, check if there is an appended script
162- var code = '' ;
163- try {
164- code = microbitFs . getIntelHexAppendedScript ( hexStr ) ;
165- // If no code is found throw a dummy error to trigger the catch below
166- if ( ! code ) throw new Error ( 'No appended code found.' ) ;
167- } catch ( e ) {
168- // This was originally config.translate.alerts.no_script
169- throw new Error ( errorMsg + 'Hex file does not contain an appended Python script.' ) ;
170- }
171- fs1 . ls ( ) . forEach ( function ( fileName ) {
172- fs1 . remove ( fileName ) ;
173- } ) ;
174- fs1 . write ( 'main.py' , code ) ;
175- fs2 . ls ( ) . forEach ( function ( fileName ) {
176- fs2 . remove ( fileName ) ;
129+ /**
130+ * Import an appended script from the provide hex string into the filesystem.
131+ * If the import is successful this deletes all the previous files.
132+ *
133+ * @param {string } hexStr Hex (Intel or Universal) string with files to
134+ * import.
135+ * @return {string[] } Array with the filenames of all files imported.
136+ */
137+ fsWrapper . importHexAppended = function ( hexStr ) {
138+ var code = microbitFs . getIntelHexAppendedScript ( hexStr ) ;
139+ if ( ! code ) {
140+ throw new Error ( 'No appended code found in the hex file' ) ;
141+ } ;
142+ uPyFs . ls ( ) . forEach ( function ( fileName ) {
143+ uPyFs . remove ( fileName ) ;
177144 } ) ;
178- fs2 . write ( 'main.py' , code ) ;
145+ uPyFs . write ( 'main.py' , code ) ;
179146 return [ 'main.py' ] ;
180147 } ;
181148
0 commit comments