Repository: source-academy/js-slang
Pull request #260 (2019-06-29 10:06:20)

Display and error

martin-henz: As per telegram discussion: This PR needs a change in the frontend, with same branch name: display-and-error.

Warning: I used --no-verify when pushing. Here is the error

```
Error: Cannot find module './plugins/immutable'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/henz/Documents/source-academy/js-slang/node_modules/pretty-format/build/index.js:25:18)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
```
coveralls: ## Pull Request Test Coverage Report for [Build 1249](https://coveralls.io/builds/24279602)

* **8** of **10**   **(80.0%)**  changed or added relevant lines in **2** files are covered.
* No unchanged relevant lines lost coverage.
* Overall coverage decreased (**-0.1%**) to **92.433%**

---

|  Changes Missing Coverage | Covered Lines | Changed/Added Lines | % |
| :-----|--------------|--------|---: |
| [src/stdlib/misc.ts](https://coveralls.io/builds/24279602/source?filename=src%2Fstdlib%2Fmisc.ts#L13) | 3 | 5 | 60.0%
<!-- | **Total:** | **8** | **10** | **80.0%** | -->


|  Totals | [![Coverage Status](https://coveralls.io/builds/24279602/badge)](https://coveralls.io/builds/24279602) |
| :-- | --: |
| Change from base [Build 1243](https://coveralls.io/builds/24278499): |  -0.1% |
| Covered Lines: | 1716 |
| Relevant Lines: | 1836 |

---
##### 💛  - [Coveralls](https://coveralls.io)

openorclose: > As per telegram discussion: This PR needs a change in the frontend, with same branch name: display-and-error.
> 
> Warning: I used --no-verify when pushing. Here is the error
> 
> ```
> Error: Cannot find module './plugins/immutable'
>     at Function.Module._resolveFilename (module.js:547:15)
>     at Function.Module._load (module.js:474:25)
>     at Module.require (module.js:596:17)
>     at require (internal/module.js:11:18)
>     at Object.<anonymous> (/Users/henz/Documents/source-academy/js-slang/node_modules/pretty-format/build/index.js:25:18)
>     at Module._compile (module.js:652:30)
>     at Object.Module._extensions..js (module.js:663:10)
>     at Module.load (module.js:565:32)
>     at tryModuleLoad (module.js:505:12)
>     at Function.Module._load (module.js:497:3)
> ```

I tried running yarn test, and did not encounter this error. Only errors I encountered were because of the test cases that haven't been updated.

Patch:
diff --git a/package.json b/package.json
index 023e5c64..4d09f192 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "js-slang",
-  "version": "0.2.2",
+  "version": "0.2.3",
   "description": "Javascript-based interpreter for slang, written in Typescript",
   "author": {
     "name": "Source Academy",
diff --git a/src/createContext.ts b/src/createContext.ts
index 5db2b3d1..d5dd5a5a 100644
--- a/src/createContext.ts
+++ b/src/createContext.ts
@@ -114,10 +114,11 @@ export const importExternalSymbols = (context: Context, externalSymbols: string[
 export const importBuiltins = (context: Context, externalBuiltIns: CustomBuiltIns) => {
   ensureGlobalEnvironmentExist(context)
 
-  const rawDisplay = (v: Value) => externalBuiltIns.rawDisplay(v, context.externalContext)
-  const display = (v: Value) => (rawDisplay(stringify(v)), v)
-  const prompt = (v: Value) => externalBuiltIns.prompt(v, context.externalContext)
-  const alert = (v: Value) => externalBuiltIns.alert(v, context.externalContext)
+  const rawDisplay = (v: Value, s: string) =>
+    externalBuiltIns.rawDisplay(v, s, context.externalContext)
+  const display = (v: Value, s: string) => (rawDisplay(stringify(v), s), v)
+  const prompt = (v: Value) => externalBuiltIns.prompt(v, '', context.externalContext)
+  const alert = (v: Value) => externalBuiltIns.alert(v, '', context.externalContext)
   const visualiseList = (v: Value) => externalBuiltIns.visualiseList(v, context.externalContext)
 
   if (context.chapter >= 1) {
diff --git a/src/stdlib/misc.ts b/src/stdlib/misc.ts
index d827fafa..f2526070 100644
--- a/src/stdlib/misc.ts
+++ b/src/stdlib/misc.ts
@@ -8,14 +8,14 @@ import { Context, Value } from '../types'
  * @param externalContext a property of Context that can hold
  *   any information required for external use (optional).
  */
-export function rawDisplay(value: Value, externalContext: any) {
+export function rawDisplay(value: Value, str: string, externalContext: any) {
   // tslint:disable-next-line:no-console
-  console.log(value.toString())
+  console.log((str === undefined ? '' : str + ' ') + value.toString())
   return value
 }
 
-export function error_message(value: Value) {
-  const output = stringify(value)
+export function error_message(value: Value, str?: string) {
+  const output = (str === undefined ? '' : str + ' ') + stringify(value)
   throw new Error(output)
 }
 
@@ -24,13 +24,13 @@ export function timed(
   // tslint:disable-next-line:ban-types
   f: Function,
   externalContext: any,
-  displayBuiltin: (value: Value, externalContext: any) => Value
+  displayBuiltin: (value: Value, str: string, externalContext: any) => Value
 ) {
   return (...args: any[]) => {
     const start = runtime()
     const result = f(...args)
     const diff = runtime() - start
-    displayBuiltin('Duration: ' + Math.round(diff) + 'ms', externalContext)
+    displayBuiltin('Duration: ' + Math.round(diff) + 'ms', '', externalContext)
     return result
   }
 }
diff --git a/src/types.ts b/src/types.ts
index e38ccf72..c8e9df2d 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -13,9 +13,9 @@ import * as es from 'estree'
  * different implementations. e.g display() in a web application.
  */
 export interface CustomBuiltIns {
-  rawDisplay: (value: Value, externalContext: any) => void
-  prompt: (value: Value, externalContext: any) => string | null
-  alert: (value: Value, externalContext: any) => void
+  rawDisplay: (value: Value, str: string, externalContext: any) => Value
+  prompt: (value: Value, str: string, externalContext: any) => string | null
+  alert: (value: Value, str: string, externalContext: any) => void
   /* Used for list visualisation. See #12 */
   visualiseList: (list: any, externalContext: any) => void
 }


Test patch:
diff --git a/src/utils/testing.ts b/src/utils/testing.ts
index 0171edfd..7914719f 100644
--- a/src/utils/testing.ts
+++ b/src/utils/testing.ts
@@ -45,9 +45,9 @@ function createTestContext({
   } else {
     const testContext: TestContext = {
       ...createContext(chapter, [], undefined, {
-        rawDisplay: (str, externalContext) => {
-          testContext.displayResult.push(str)
-          return str
+        rawDisplay: (str1, str2, externalContext) => {
+          testContext.displayResult.push((str2 === undefined ? '' : str2 + ' ') + str1)
+          return str1
         },
         prompt: (str, externalContext) => {
           testContext.promptResult.push(str)