implement arg-modifying support for swift debugging
diff --git a/client/lib/debug-bootstrap.swift b/client/lib/debug-bootstrap.swift
index e4581b0..8c2f791 100644
--- a/client/lib/debug-bootstrap.swift
+++ b/client/lib/debug-bootstrap.swift
@@ -22,10 +22,10 @@
};*/
func bootstrap(key: String, namespace: String, triggerName: String,
- main: ([String:Any]) -> [String:Any],
- actualParameters: [String:Any]) {
+ main: (inout [String:Any]) -> [String:Any],
+ actualParameters: inout [String:Any]) {
- let result = main(actualParameters);
+ let result = main(&actualParameters);
// print("Returning \(result)");
diff --git a/client/lib/debug-swift.js b/client/lib/debug-swift.js
index 8bf846f..1b62cb8 100644
--- a/client/lib/debug-swift.js
+++ b/client/lib/debug-swift.js
@@ -21,37 +21,36 @@
path = require('path'),
spawn = require('child_process').spawn;
-function compileIt(sourcePath) {
+function compileIt(sourcePath, tmpDirPath, actionName) {
// console.log("COMPILEIT", sourcePath);
return new Promise((resolve, reject) => {
- tmp.file(function onTempFileCreation(err, executablePath, fd, executableCleanup) {
- var spawnOpts = {
- cwd: process.cwd(),
- stdio: ['inherit', 'inherit', 'inherit'],
- env: process.env
- };
- try {
- var child = spawn('swiftc',
- ['-o', executablePath,
- '-g',
- sourcePath],
- spawnOpts);
- child.on('exit', (code) => {
- if (code !== 0) {
- reject(code);
- } else {
- resolve(executablePath, executableCleanup);
- }
- });
- } catch (err) {
- console.error(err);
- reject(err);
- }
- });
+ var spawnOpts = {
+ cwd: tmpDirPath,
+ stdio: ['inherit', 'inherit', 'inherit'],
+ env: process.env
+ };
+ try {
+ var executablePath = path.join(tmpDirPath, actionName);
+ var child = spawn('swiftc',
+ ['-o', executablePath,
+ '-g',
+ sourcePath],
+ spawnOpts);
+ child.on('exit', (code) => {
+ if (code !== 0) {
+ reject(code);
+ } else {
+ resolve(executablePath);
+ }
+ });
+ } catch (err) {
+ console.error(err);
+ reject(err);
+ }
});
}
-function debugIt(eventBus, executablePath, executableCleanup) {
+function debugIt(eventBus, executablePath) {
// console.log("DEBUGIT", executablePath);
return new Promise((resolve, reject) => {
try {
@@ -116,7 +115,11 @@
exports._debug = function debugSwift(message, ws, echoChamberNames, done, commandLineOptions, eventBus) {
var code = message.action.exec.code;
- var r = new RegExp(/main[\s]*\([^\)]*\)/);
+ var r2 = new RegExp(/\[[\s]*String[\s]*:[\s]*Any[\s]*\]/);
+ var paramType = code.search(r2, startOfMethodBody);
+ code = code.substring(0, paramType) + ' inout ' + code.substring(paramType);
+
+ var r = new RegExp(/func main[\s]*\([^\)]*\)/);
var startOfMethodBody = code.search(r);
if (startOfMethodBody >= 0) {
var paren = code.indexOf('{', startOfMethodBody);
@@ -124,33 +127,36 @@
}
fs.readFile(path.join('lib', 'debug-bootstrap.swift'), (err, codeBuffer) => {
- code += '\n\n//\n';
- code += '// Welcome to the OpenWhisk debugger.\n';
- code += '//\n';
- code += '// To proceed with debugging, press the continue => button.\n';
- code += '// The first breakpoint will be in your main method\n';
- code += '//\n';
+ code += '\n\n//\n';
+ code += '// Welcome to the OpenWhisk debugger.\n';
+ code += '//\n';
+ code += '// To proceed with debugging, press the continue => button.\n';
+ code += '// The first breakpoint will be in your main method\n';
+ code += '//\n';
code += codeBuffer.toString('utf8');
- code += '\nbootstrap(key: "' + message.key + '", namespace: "' + message.action.namespace + '", triggerName: "' + echoChamberNames.trigger + '", main: main, actualParameters: ' + jsonToSwiftDictionary(message.actualParameters) + ');';
+ code += '\nvar params: [String:Any] = ' + jsonToSwiftDictionary(message.actualParameters);
+ code += '\nbootstrap(key: "' + message.key + '", namespace: "' + message.action.namespace + '", triggerName: "' + echoChamberNames.trigger + '", main: main, actualParameters: ¶ms);';
- tmp.file({ postfix: '.swift' }, function onTempFileCreation(err, tmpFilePath, fd, tmpfileCleanupCallback) {
- // console.log('TMP ' + tmpFilePath);
- try {
- fs.write(fd, code, 0, 'utf8', function onFileWriteCompletion(err, written, string) {
+ tmp.dir({ prefix: 'wskdb-', unsafeCleanup: true }, function onTempDirCreation(err, tmpDirPath, fd, tmpdirCleanupCallback) {
+ var tmpFilePath = path.join(tmpDirPath, message.action.name + '.swift');
+ console.log('TMP ' + tmpFilePath);
- compileIt(tmpFilePath)
- .then(debugIt.bind(undefined, eventBus))
- .then(() => {
- try { tmpfileCleanupCallback(); } catch (e) { }
- done(); // we don't need to "ok" here, as the invoker will do that for us
- });
- });
- } catch (err) {
- console.error(err);
- console.error(err.stack);
- try { tmpfileCleanupCallback(); } catch (e) { }
- done();
- }
- });
+ try {
+ fs.writeFile(tmpFilePath, code, 0, 'utf8', function onFileWriteCompletion(err, written, string) {
+
+ compileIt(tmpFilePath, tmpDirPath, message.action.name)
+ .then(debugIt.bind(undefined, eventBus))
+ .then(() => {
+ try { tmpdirCleanupCallback(); } catch (e) { }
+ done(); // we don't need to "ok" here, as the invoker will do that for us
+ });
+ });
+ } catch (err) {
+ console.error(err);
+ console.error(err.stack);
+ try { tmpdirCleanupCallback(); } catch (e) { }
+ done();
+ }
+ });
});
};