I will show how one can continue to use V8 even after we stop embedding it (can do this right now, too) using an example from this thread: Accessing turf.js functions in queries To recap, there is a Javascript library named turf.js, it contains various geometry functions, the task is to call the function that generates a hexagonal grid and output this into a drawing. The original code ran turf.js using V8. JScript.NET cannot run it because the language version it supports is fairly old and turf.js uses features added in later versions. In the next build we are going to stop embedding V8. However, you can still use V8 to run turf.js and similar code, here's how. There is a .NET project for running V8 (and some other) scripts, called ClearScript (github here). This is a Microsoft project, well-maintained and kept up to date. The attached ZIP archive contains the latest ClearScript DLLs. Unpack the archive, then copy all DLLs except ClearScriptV8.win-x64.dll (leaving it will do no harm) into ~\bin and all DLLs except ClearScriptV8.win-x86.dll into ~\bin64. Then launch a new instance of Manifold, open the attached MXB. Open the drawing, observe that it is empty. Now run the query, it should populate the drawing with a hexagonal grid created using turf.js. Here's how it works: The comments component, Turf_script, contains the text of turf.js. This is taken verbatim from the URL referenced in examples on turfjs.org: https://unpkg.com/@turf/turf@6.3.0/turf.min.js There are no modifications whatsoever, the text is simply copied over as is. The script component, Script, contains this: // C# // // $reference: Microsoft.Csharp.dll // $reference: System.Core.dll // $reference: ClearScript.Core.dll // $reference: ClearScript.V8.dll // $reference: ClearScript.Windows.Core.dll // $reference: ClearScript.Windows.dll // using System; using Microsoft.ClearScript; using Microsoft.ClearScript.JavaScript; using Microsoft.ClearScript.V8; class Script { public static string HexGrid( double xmin, double ymin, double xmax, double ymax, double cellSide) { Manifold.Application app = Manifold.Application; Manifold.Database db = app.GetDatabaseRoot(); using (V8ScriptEngine engine = new V8ScriptEngine()) { engine.Execute(db.GetProperty("Turf_script", "Text")); engine.Execute(string.Format( "var grid = " + "JSON.stringify(turf.hexGrid([{0}, {1}, {2}, {3}], {4}).features);", xmin, ymin, xmax, ymax, cellSide)); return engine.Script.grid; } } static Manifold.Context Manifold; static void Main() { } } The Main function does nothing. We are using the script for its HexGrid function which takes the parameters for the grid and returns it as a JSON. The script references ClearScript DLLs using $reference. HexGrid creates an instance of V8. Then it tells V8 to run turf.js, taking the source code for it from the comments component. Then it tells V8 to run the line: 'var grid = JSON.stringify(turf.hexGrid(...));' putting the parameters into the string. After this line finishes running, the generated geometry is in the variable named 'grid' inside the V8 script. The function then returns the value in that variable to the caller. The query component, Query, contains this: --SQL9 FUNCTION hexGrid( @xmin FLOAT64, @ymin FLOAT64, @xmax FLOAT64, @ymax FLOAT64, @cell FLOAT64) NVARCHAR AS SCRIPT [Script] ENTRY 'Script.HexGrid'; DELETE FROM [drawing]; INSERT INTO [drawing] ([geom]) SELECT StringJsonGeoGeom(StringJsonValue(value, 'geometry', false)) FROM CALL StringToJsonArrayValues(hexGrid(-95, 30 ,-85, 40, 50)); We first declare that we are going to use a function named Script.HexGrid from the component Script. Then we delete all records from Drawing. Then we call the function, split the results into individual strings for each returned polygon, convert each string into a GEOM, and insert that into Drawing. We can also modify Script to download the contents of turf.js from the URL automatically (and put it into a corresponding comments component if it has not been created yet). Hope this helps. Attachments: clearscript-dlls.zip turfjs-clearscript.mxb
|