git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Change FFI loading scheme
434ac1aAndy Arvanitis
runtime/purescript.go+13−2
@@ -26,6 +26,17 @@ type Fn10 = func(Any, Any, Any, Any, Any, Any, Any, Any, Any, Any) Any
26
26
27const Undefined = "undefined"
27const Undefined = "undefined"
28
28
29+var foreign = make(map[string]Map)
30+
31+func Foreign(key string) Map {
32+ value, found := foreign[key]
33+ if !found {
34+ value = make(Map)
35+ foreign[key] = value
36+ }
37+ return value
38+}
39+
29func Apply(f Any, args ...Any) Any {
40func Apply(f Any, args ...Any) Any {
30 result := f
41 result := f
31 for _, arg := range args {
42 for _, arg := range args {
@@ -40,10 +51,10 @@ func Run(f Any, args ...Any) Any {
40 return fn()
51 return fn()
41}
52}
42
53
43−func SafeGet(dict map[string]Any, key string) Any {
54+func Get(dict map[string]Any, key string) Any {
44 value, ok := dict[key]
55 value, ok := dict[key]
45 if !ok {
56 if !ok {
46− panic("Foreign value '" + key + "' not defined")
57+ panic("Foreign value '" + key + "' not found")
47 }
58 }
48 return value
59 return value
49}
60}
src/CodeGen/IL.hs+1−1
@@ -63,7 +63,7 @@ moduleToIL (Module _ coms mn _ imps _ foreigns decls) _ =
63 interface = interfaceSource modName values foreigns
63 interface = interfaceSource modName values foreigns
64 imports = nub . concat $ importToIL <$> optimized'
64 imports = nub . concat $ importToIL <$> optimized'
65 implHeader = implHeaderSource modName imports ""
65 implHeader = implHeaderSource modName imports ""
66− implFooter = implFooterSource modName foreigns
66+ implFooter = implFooterSource (runModuleName mn) foreigns
67 return $ (interface, foreigns', optimized', implHeader, implFooter)
67 return $ (interface, foreigns', optimized', implHeader, implFooter)
68 where
68 where
69 modName = moduleNameToIL mn
69 modName = moduleNameToIL mn
src/CodeGen/IL/Printer.hs+6−3
@@ -456,7 +456,7 @@ implFooterSource mn foreigns =
456 (if null foreigns
456 (if null foreigns
457 then ""
457 then ""
458 else ("// Foreign values\n\n" <>
458 else ("// Foreign values\n\n" <>
459− "var " <> foreignDict <> " = " <> dictType <> "{}\n\n" <>
459+ "var " <> foreignDict <> " = "<> foreignMod <> "(\"" <> mn <> "\")\n\n" <>
460 (T.concat $ (\foreign' ->
460 (T.concat $ (\foreign' ->
461 let name = moduleIdentToIL foreign' in
461 let name = moduleIdentToIL foreign' in
462 varDecl <> " " <> initName name <> " Once\n" <>
462 varDecl <> " " <> initName name <> " Once\n" <>
@@ -466,7 +466,7 @@ implFooterSource mn foreigns =
466 "() Any { \n" <>
466 "() Any { \n" <>
467 " " <> initName name <> ".Do(func() {\n" <>
467 " " <> initName name <> ".Do(func() {\n" <>
468 " " <> valueName name <> " = " <>
468 " " <> valueName name <> " = " <>
469− "SafeGet(" <> foreignDict <> ", " <>
469+ "Get(" <> foreignDict <> ", " <>
470 (stringLiteral . mkString $ runIdent foreign') <> ")\n" <>
470 (stringLiteral . mkString $ runIdent foreign') <> ")\n" <>
471 " })\n" <>
471 " })\n" <>
472 " return " <> valueName name <> "\n" <>
472 " return " <> valueName name <> "\n" <>
@@ -486,8 +486,11 @@ varDecl = "var"
486renderArg :: Text -> Text -> Text
486renderArg :: Text -> Text -> Text
487renderArg decl arg = arg <> " " <> decl
487renderArg decl arg = arg <> " " <> decl
488
488
489+foreignMod :: Text
490+foreignMod = "Foreign"
491+
489foreignDict :: Text
492foreignDict :: Text
490−foreignDict = "Foreign"
493+foreignDict = "foreign"
491
494
492initName :: Text -> Text
495initName :: Text -> Text
493initName s = "ₒ" <> s
496initName s = "ₒ" <> s