mirror of
https://github.com/encounter/decomp.me.git
synced 2026-03-30 11:06:27 -07:00
Scratch updating, fix various bugs, toast
This commit is contained in:
@@ -59,6 +59,11 @@ class AsmDifferWrapper:
|
||||
config = AsmDifferWrapper.create_config(MIPS_SETTINGS) # todo read arch from compiler config of compilation
|
||||
basedump = AsmDifferWrapper.run_objdump(target_assembly.object, config)
|
||||
mydump = AsmDifferWrapper.run_objdump(compilation.object, config)
|
||||
|
||||
# Remove first few junk lines from objdump output
|
||||
basedump = "\n".join(basedump.split("\n")[6:])
|
||||
mydump = "\n".join(mydump.split("\n")[6:])
|
||||
|
||||
display = Display(basedump, mydump, config)
|
||||
|
||||
return display.run_diff()
|
||||
|
||||
@@ -3,6 +3,7 @@ from django.conf import settings
|
||||
from django.utils.crypto import get_random_string
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
|
||||
@@ -23,6 +24,10 @@ def asm_objects_path() -> Path:
|
||||
def compilation_objects_path() -> Path:
|
||||
return Path(os.path.join(settings.LOCAL_FILE_DIR, 'compilations'))
|
||||
|
||||
def replace_paths(match) -> str:
|
||||
s = match.group(0)
|
||||
return "src" + s[-2:]
|
||||
|
||||
class CompilerWrapper:
|
||||
def base_path():
|
||||
return settings.COMPILER_BASE_PATH
|
||||
@@ -44,14 +49,9 @@ class CompilerWrapper:
|
||||
result = subprocess.run(compile_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
|
||||
stderr = result.stderr.decode()
|
||||
|
||||
stderr_lines = stderr.splitlines()
|
||||
for i, line in enumerate(stderr_lines):
|
||||
file_end = line.find(":")
|
||||
|
||||
if file_end != -1:
|
||||
stderr_lines[i] = "input.c" + line[file_end:].strip()
|
||||
|
||||
stderr = "\n".join(stderr_lines)
|
||||
pattern = re.compile(r"[/](?:(?!\.\s+)\S)+(\.)?")
|
||||
stderr = re.sub(pattern, replace_paths, stderr)
|
||||
|
||||
if result.returncode != 0:
|
||||
logger.error(result.stderr.decode())
|
||||
@@ -132,7 +132,7 @@ class CompilerWrapper:
|
||||
object_path = compilation_objects_path() / (temp_name + ".o")
|
||||
|
||||
with open(code_path, "w", newline="\n") as f:
|
||||
f.write(context + code)
|
||||
f.write(context + code + "\n")
|
||||
|
||||
# Run compiler
|
||||
compile_status, stderr = CompilerWrapper.run_compiler(
|
||||
|
||||
+10
-12
@@ -76,7 +76,6 @@ def scratch(request, slug=None):
|
||||
asm = get_db_asm(data["target_asm"])
|
||||
del data["target_asm"]
|
||||
|
||||
# Validate target asm
|
||||
compiler_config = CompilerConfiguration.objects.get(id=request.data["compiler_config"])
|
||||
|
||||
assembly = CompilerWrapper.assemble_asm(compiler_config, asm)
|
||||
@@ -98,17 +97,16 @@ def scratch(request, slug=None):
|
||||
if not slug:
|
||||
return Response({"error": "Missing slug"}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
if "compiler_config" not in request.data:
|
||||
return Response({"error": "Missing compiler_config"}, status=status.HTTP_400_BAD_REQUEST)
|
||||
required_params = ["compiler_config", "source_code", "context"]
|
||||
|
||||
if "source_code" not in request.data:
|
||||
return Response({"error": "Missing source_code"}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
if "context" not in request.data:
|
||||
return Response({"error": "Missing context"}, status=status.HTTP_400_BAD_REQUEST)
|
||||
for param in required_params:
|
||||
if param not in request.data:
|
||||
return Response({"error": f"Missing parameter: {param}"}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
compiler_config = CompilerConfiguration.objects.get(id=request.data["compiler_config"])
|
||||
|
||||
db_scratch = get_object_or_404(Scratch, slug=slug)
|
||||
db_scratch.compiler_config = request.data["compiler_config"]
|
||||
db_scratch.compiler_config = compiler_config
|
||||
db_scratch.source_code = request.data["source_code"]
|
||||
db_scratch.context = request.data["context"]
|
||||
db_scratch.save()
|
||||
@@ -116,21 +114,21 @@ def scratch(request, slug=None):
|
||||
|
||||
@api_view(["POST"])
|
||||
def compile(request, slug):
|
||||
required_params = ["compiler_config", "code", "context"]
|
||||
required_params = ["compiler_config", "source_code", "context"]
|
||||
|
||||
for param in required_params:
|
||||
if param not in request.data:
|
||||
return Response({"error": f"Missing parameter: {param}"}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
compiler_config = CompilerConfiguration.objects.get(id=request.data["compiler_config"])
|
||||
code = request.data["code"]
|
||||
code = request.data["source_code"]
|
||||
context = request.data["context"]
|
||||
scratch = Scratch.objects.get(slug=slug)
|
||||
|
||||
compilation, errors = CompilerWrapper.compile_code(compiler_config, code, context)
|
||||
|
||||
diff_output = ""
|
||||
if not errors:
|
||||
if compilation:
|
||||
diff_output = AsmDifferWrapper.diff(scratch.target_assembly, compilation)
|
||||
|
||||
response_obj = {
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
"@monaco-editor/react": "^4.2.1",
|
||||
"preact": "^10.5.13",
|
||||
"preact-router": "^3.2.1",
|
||||
"react-hot-toast": "^2.1.0",
|
||||
"react-simple-resizer": "^2.1.0",
|
||||
"swr": "^0.5.6",
|
||||
"use-debounce": "^7.0.0"
|
||||
|
||||
@@ -40,3 +40,26 @@ export async function post(url, body) {
|
||||
|
||||
return await response.json()
|
||||
}
|
||||
|
||||
export async function patch(url, body) {
|
||||
if (typeof body != "string") {
|
||||
body = JSON.stringify(body)
|
||||
}
|
||||
|
||||
console.info("PATCH", url, JSON.parse(body))
|
||||
|
||||
const response = await fetch(API_BASE + url, {
|
||||
...commonOpts,
|
||||
method: "PATCH",
|
||||
body,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(response.status)
|
||||
}
|
||||
|
||||
return await response.json()
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { h } from "preact"
|
||||
import { useEffect, useState } from "preact/hooks"
|
||||
import { useDebouncedCallback } from "use-debounce"
|
||||
import * as resizer from "react-simple-resizer"
|
||||
import toast, { Toaster } from 'react-hot-toast';
|
||||
|
||||
import * as api from "../api"
|
||||
import CompilerConfigSelect from "./CompilerConfigSelect"
|
||||
@@ -30,7 +31,7 @@ export default function Scratch({ slug }) {
|
||||
const compile = async () => {
|
||||
const { diff_output, errors } = await api.post(`/scratch/${slug}/compile`, {
|
||||
compiler_config: compilerConfig,
|
||||
code: cCode,
|
||||
source_code: cCode,
|
||||
context: cContext,
|
||||
})
|
||||
|
||||
@@ -41,6 +42,19 @@ export default function Scratch({ slug }) {
|
||||
}
|
||||
}
|
||||
|
||||
const update = async () => {
|
||||
const { errors } = await api.patch(`/scratch/${slug}`, {
|
||||
compiler_config: compilerConfig,
|
||||
source_code: cCode,
|
||||
context: cContext,
|
||||
})
|
||||
.then(
|
||||
toast.success("Scratch updated!", {position: "top-right"})
|
||||
)
|
||||
|
||||
setLog(errors)
|
||||
}
|
||||
|
||||
// Recompile automatically
|
||||
const debounced = useDebouncedCallback(compile, 1000)
|
||||
|
||||
@@ -58,6 +72,7 @@ export default function Scratch({ slug }) {
|
||||
})
|
||||
|
||||
return <div class={styles.container}>
|
||||
<Toaster />
|
||||
<div class={styles.toolbar}>
|
||||
<CompilerConfigSelect
|
||||
value={compilerConfig}
|
||||
@@ -68,6 +83,7 @@ export default function Scratch({ slug }) {
|
||||
/>
|
||||
|
||||
<button onClick={compile} class={styles.compile}>compile</button>
|
||||
<button onClick={update} class={styles.compile}>update</button>
|
||||
</div>
|
||||
|
||||
<resizer.Container class={styles.resizer}>
|
||||
|
||||
@@ -772,6 +772,11 @@ globals@^11.1.0:
|
||||
resolved "https://registry.yarnpkg.com/globals/-/globals-11.12.0.tgz#ab8795338868a0babd8525758018c2a7eb95c42e"
|
||||
integrity sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA==
|
||||
|
||||
goober@^2.0.35:
|
||||
version "2.0.39"
|
||||
resolved "https://registry.yarnpkg.com/goober/-/goober-2.0.39.tgz#0a52bf78ee9562270669e7603f0250f014f37cb1"
|
||||
integrity sha512-ryw0VaZaehKmnjL4ZEJaiUVQc+XFa5dXIAbf2QC3F+WVKRbzaSuJyq7w28bdlwqYctiZ0Ok5QL/Pap5M2pCHQg==
|
||||
|
||||
graceful-fs@^4.2.3:
|
||||
version "4.2.6"
|
||||
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.6.tgz#ff040b2b0853b23c3d31027523706f1885d76bee"
|
||||
@@ -1397,6 +1402,13 @@ qs@~6.5.2:
|
||||
resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.2.tgz#cb3ae806e8740444584ef154ce8ee98d403f3e36"
|
||||
integrity sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA==
|
||||
|
||||
react-hot-toast@^2.1.0:
|
||||
version "2.1.0"
|
||||
resolved "https://registry.yarnpkg.com/react-hot-toast/-/react-hot-toast-2.1.0.tgz#f4fe3107a3ccd1d7e76ad3d3282967fbf2fccf2c"
|
||||
integrity sha512-/wUoUVUC/qCYmkJvMzl/oXBIJe47vYqtwOpA76Pz8SrklrF8RBUTLG2eS+ivznzQzY8YlsyODIV7Qk3vOnFf1g==
|
||||
dependencies:
|
||||
goober "^2.0.35"
|
||||
|
||||
react-is@^16.8.1:
|
||||
version "16.13.1"
|
||||
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
|
||||
|
||||
Reference in New Issue
Block a user