Creating a JS Console

As a little example, we will create a JS console. We need an input field where the user can enter his JS expressions and ideally there should be a list of output results. As this should more look like a desktop application we use the Qt Quick Controls module.

TIP

A JS console inside your next project can be really beneficial for testing. Enhanced with a Quake-Terminal effect it is also good to impress customers. To use it wisely you need to control the scope the JS console evaluates in, e.g. the currently visible screen, the main data model, a singleton core object or all together.

image

We use Qt Creator to create a Qt Quick UI project using Qt Quick controls. We call the project JSConsole. After the wizard has finished we have already a basic structure for the application with an application window and a menu to exit the application.

For the input, we use a TextField and a Button to send the input for evaluation. The result of the expression evaluation is displayed using a ListView with a ListModel as the model and two labels to display the expression and the evaluated result.

  1. // part of JSConsole.qml
  2. ApplicationWindow {
  3. id: root
  4. ...
  5. ColumnLayout {
  6. anchors.fill: parent
  7. anchors.margins: 9
  8. RowLayout {
  9. Layout.fillWidth: true
  10. TextField {
  11. id: input
  12. Layout.fillWidth: true
  13. focus: true
  14. onAccepted: {
  15. // call our evaluation function on root
  16. root.jsCall(input.text)
  17. }
  18. }
  19. Button {
  20. text: qsTr("Send")
  21. onClicked: {
  22. // call our evaluation function on root
  23. root.jsCall(input.text)
  24. }
  25. }
  26. }
  27. Item {
  28. Layout.fillWidth: true
  29. Layout.fillHeight: true
  30. Rectangle {
  31. anchors.fill: parent
  32. color: '#333'
  33. border.color: Qt.darker(color)
  34. opacity: 0.2
  35. radius: 2
  36. }
  37. ScrollView {
  38. id: scrollView
  39. anchors.fill: parent
  40. anchors.margins: 9
  41. ListView {
  42. id: resultView
  43. model: ListModel {
  44. id: outputModel
  45. }
  46. delegate: ColumnLayout {
  47. width: ListView.view.width
  48. Label {
  49. Layout.fillWidth: true
  50. color: 'green'
  51. text: "> " + model.expression
  52. }
  53. Label {
  54. Layout.fillWidth: true
  55. color: 'blue'
  56. text: "" + model.result
  57. }
  58. Rectangle {
  59. height: 1
  60. Layout.fillWidth: true
  61. color: '#333'
  62. opacity: 0.2
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }

The evaluation function jsCall does the evaluation not by itself this has been moved to a JS module (jsconsole.js) for clearer separation.

  1. // part of JSConsole.qml
  2. import "jsconsole.js" as Util
  3. ...
  4. ApplicationWindow {
  5. id: root
  6. ...
  7. function jsCall(exp) {
  8. var data = Util.call(exp);
  9. // insert the result at the beginning of the list
  10. outputModel.insert(0, data)
  11. }
  12. }

For safety, we do not use the eval function from JS as this would allow the user to modify the local scope. We use the Function constructor to create a JS function on runtime and pass in our scope as this variable. As the function is created every time it does not act as a closure and stores its own scope, we need to use this.a = 10 to store the value inside this scope of the function. This scope is set by the script to the scope variable.

  1. // jsconsole.js
  2. .pragma library
  3. var scope = {
  4. // our custom scope injected into our function evaluation
  5. }
  6. function call(msg) {
  7. var exp = msg.toString();
  8. console.log(exp)
  9. var data = {
  10. expression : msg
  11. }
  12. try {
  13. var fun = new Function('return (' + exp + ');');
  14. data.result = JSON.stringify(fun.call(scope), null, 2)
  15. console.log('scope: ' + JSON.stringify(scope, null, 2) + 'result: ' + result)
  16. } catch(e) {
  17. console.log(e.toString())
  18. data.error = e.toString();
  19. }
  20. return data;
  21. }

The data return from the call function is a JS object with a result, expression and error property: data: { expression: {}, result: {}, error: {} }. We can use this JS object directly inside the ListModel and access it then from the delegate, e.g. model.expression gives us the input expression. For the simplicity of the example, we ignore the error result.